Need help with WebUserControl
Hello.
I'm having some trouble with webusercontrols. I've created one with a checkbox and a textbox. Now, I want to create three of these when my page loads. I'm using this code for that (I'm also using a placeholder):
Code:
WebUserAlternative uc1;
for (int i = 0; i < 3; i++)
{
uc1 = (WebUserAlternative)Page.LoadControl("~/WebUserAlternative.ascx");
uc1.ID = "nr" + i;
PlaceHolder1.Controls.Add(uc1);
}
Now to my problem.
How do I check the text that has been written in each of those textboxes (the textbox inside the webusercontrol) and if the checkboxes are checked?
I know how to do this when I "pre-create" (not on the fly) one webusercontrol, but I can't figure out how to get the text from all the textboxes etc that are created on page_load.
Damn, that was messy :) Anyway.. I hope you guys understand my problem.
Re: Need help with WebUserControl
Have you created properties in your usercontrol to access these values already?
Properties in usercontrol...
Code:
public string EmailAddress
{
get { return this.txtEmail.Text.Trim(); }
set { this.txtEmail.Text = value; }
}
To access dynamic controls at runtime
Code:
foreach (Control c in this.placeholder.Controls)
{
if (c.GetType().ToString() == "ASP.mycontrol_ascx")
{
myString = ((mycontrol)c).EmailAddress;
}
}
Hope that helps.
Re: Need help with WebUserControl
Re: Need help with WebUserControl
Btw... I tried to put this code in a dropDownList_SelectedIndexChanged method. But that doesn't work. It only seems to work when I put it in page_load.
Does anyone know how to fix that?
It's not that important... I'm just curious as to why it doesn't work.
Re: Need help with WebUserControl
Because selecting an index of DropDownList doesn't fire the event. Place a Breakpoint inside dropDownList_SelectedIndexChanged method, run the application and change its index.
Change the AutoPostBack property of dropDownList to True, it will give you the desired result.
Re: Need help with WebUserControl
Thanks for answering, but I've already tried that. Doesn't work (at least for me it doesn't). Oh well.. I'll play around with it some more and see what I can come up with.
Re: Need help with WebUserControl
Quote:
Originally Posted by korven
Btw... I tried to put this code in a dropDownList_SelectedIndexChanged method. But that doesn't work. It only seems to work when I put it in page_load.
Does anyone know how to fix that?
It's not that important... I'm just curious as to why it doesn't work.
What do you mean exactly when you say it doesn't work?
Are you re-creating your controls on each page load?
Re: Need help with WebUserControl
Does the SelectedIndexChanged event actually get hit?