C# .NET - Need to dynamically create controls base on dropdownlist selection
Asked By Ronald Thomas
03-Nov-09 12:59 PM

I wish to allow the user to determine how many controls to generate based on their selection in the dropdownlist. My issue is that the value selected from the dropdownlist is available only after onselectedindexchanged is triggered. So I getting the value after the screen is rendered. Is there a way to obtain the selected value before onselectedindexchanged is triggered. The current code is generating the dynamic controls but only after I select a different item in the dropdownlist. Any HELP would be greatly appreciated!!! Here is my code snippets:
ASPX:
<
form id="form1" runat="server">
Number of Children controls?
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="True" EnableViewState="true" onselectedindexchanged="DDL1_SelectedIndexChanged"
>
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
</asp:DropDownList>
<br />
<div>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="myPlaceHolder1" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DDL1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
ASPX.CS
protected
override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateChildControls();
}
protected
void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
myCount = DDL1.SelectedIndex; // myCount represents the # of rows of dynamic textboxes
}
public
void CreateChildControls()
{
for (int r = 0; r < myCount; r++)
{
Child.DynamicTB =
new TextBox[myCount, 6];
for (int c = 0; c < 6; c++)
{
TextBox textbox = new TextBox();
textbox.ID =
"myTextbox" + r.ToString() + c.ToString();
myPlaceHolder1.Controls.Add(textbox);
Child.DynamicTB[r, c] = textbox;
}
}
}
This article deals with this exact scenario

Triggering Validation in ASCX control from ASPX page I have an ASCX control with validation controls in it. This control is on an ASPX page. I want the submit button on a button that is on the aspx page. Check the controlToValidate property of your validation controls on your ascx. If possible please provide your html of both ascx and aspx, we 95%;height:100%;border:0" width = "100%" > < tr > < td style = "border:0" > text 1 < asp:TextBox ID = "txt1" runat = "server" Width = "60px" AutoPostBack = "true" > < / asp:TextBox > < asp:RequiredFieldValidator ID = "required_txt1" runat = "server" ErrorMessage = "Please enter the text." ControlToValidate = "txt1" EnableClientScript = "false" Enabled = "false" Display = "Dynamic" SetFocusOnError = "true" ValidationGroup = "Submit" > *< / asp:RequiredFieldValidator > < br / > < asp:RangeValidator ID = "validator_txt1" runat = "server" ErrorMessage = "Please Double" SetFocusOnError = "True" > < / asp:RangeValidator > < / td > < / tr > < tr > < td style = "border:0" > text 2 < asp:TextBox ID = "txt2" runat = "server" Width = "70px" AutoPostBack = "true" > < / asp:TextBox > < asp:RequiredFieldValidator ID = "required_txt2" runat = "server" Enabled = "false" EnableClientScript = "false" ErrorMessage = "Please enter the text 2." ControlToValidate = "txt2" SetFocusOnError = "true" Display = "Dynamic" ValidationGroup = "Submit" > *< / asp:RequiredFieldValidator > < asp:RangeValidator ID = "validator_txt2" runat = "server" ErrorMessage = "Please enter a number
Select the data from dropdownlist or textbox I have designed one application from in the asp.net. In that form one field, suppose KitType contains both dropdownlist and textbox to enter the value. I have added some values to the dropdownlist. Now what i want is, if the value what i want is not present in the dropdownlist, i will enter the value in the textbox. Now how can i save either of the value into the database. How can i we will be seeing this type of option. you can write code this way < asp : DropDownList ID = "DropDownList1" runat = "server" AutoPostBack = "true" onselectedindexchanged = "DropDownList2_SelectedIndexChanged" > < asp : ListItem > India < / asp : ListItem > < asp : ListItem
ASP.NET Dynamic TextBox & DropDownList Control ViewState To Store NameValueCollection This article demonstrates the ASP.NET page life cycle, dynamic controls, wiring up dynamic events, ViewState, and using the NameValueCollection. Download C# Source Code Only a developer would know post back? As an added benefit, it would be nice if we could rebuild the dynamic controls in their entirety (including colors, fonts, styles, etc. . .) without requerying the database at all! This
Accessing a Dynamic Text Box inside Dynamic DropDownlist SelectedIndexChanged Event Hi Friends, I am a creating a array of dynamic Controls and Recreating them at page load by using array of controls. I am adding a dynamic event handler for dynamic dropdownlist. I want to enable or disable one of the textbox
your user control at runtime whichever you need. UserControls_header3 uh3 = ( UserControls_header3 ) this . LoadControl ( header3 ); phHeaderControls . Controls . Add ( uh3 ); HI This is the classic tear-your-hair-out problem with ASP.Net event to set up your page. 2) Implement your own ViewState handling for your user controls. This requires digging into the ViewState documentation and overriding a number of methods. 3) Joel explanation http: / / stackoverflow.com / questions / 987680 / dynamically-change-user-control-in-asp-net ► Take a DropDownlist ► Add the list of userControls: ► Set the property of Dropdownlist 's property AutoPostBaco = true ► Implement the Dropdownlist's selectedIndexChanged Event protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e) { string item = DropDownList1.SelectedItem.ToString(); switch (item) { case "Login" : { Control ucLogin = LoadControl( "Login.asax this .Form.Controls.Add(ucLogin); break ; } case "Result" : { Control ucResult = LoadControl( "Result.asax" ); this .Form.Controls.Add(ucResult