Hi. :wave:
I created a user control (3 text boxes and a drop down list) that allows users to add information. The number of user controls that must be added to the .aspx page is variable (1 to 8) and the information is validated by a separate function when the user clicks on the btnSubmit (on the .aspx Page). The code below adds the correct number of user controls to the .aspx page in a Panel control.
// Show data entry controls to match team size
for (int i=0; i <=numTeam - 1; i++)
{
Register reg = (Register)LoadControl("Register.ascx");
((Register)reg).ID = "Register" + (i).ToString();
Panel1.Controls.Add((Register)reg);
}
The code below is in the Validate routine that is called from the btnSubmit_click event.
private bool ValidateDataEntry()
{
bool valid = true;
if(Page.IsValid)
{
Register r = null;
for(int i = 0; i <= numTeam - 1; i++)
{
r = (Register)(FindControl("Register" + (i).ToString()));
if(!(r == null))
{
if(r.FName.ToString() == "" || r.LName.ToString() = ""
|| r.Gender.ToString() == "")
{
valid = false;
break;
}
f(txtEmail.Text == "")
{
valid = false;
break;
}
}
}
else
{
valid = false;
}
return valid;
}
The problem I am having is that when the validation routine runs the user controls are no longer in the Panel controls collection. When I ran trace on the code the controls showed up as child controls of the Panel control when the user controls were added. They were also listed in the Locals window of the IDE when debugging. When the btnSubmit is clicked however, they no longer show up in trace as children of the Panel control. They do appear under the trace Form Controls Collection header with the user entered information but I am not able to access them using FindControl (using either Page.FindControl or Panel1.FindControl) I also can't find them recursively looping through the Page and all of its controls.
The code listed above throws no errors, it just cannot locate the controls.
Can anyone tell me where the controls have gone and how I can access them so I can validate the information the user entered?
FYI - When I added the controls to the Page in design time (w/o the Panel control) and accessed them by name in the validation routine everything worked fine including the subsequent update to the database.
Thanks.It sounds like you are missing this from the Page_Load event:If Me.IsPostBack = True Then Exit SubI could be wrong, but it sure sounds like this is the problem.
.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment