Need to dynamically create controls base on dropdownlist selection

Asked By Ronald Thomas
03-Nov-09 12:59 PM
Earn up to 0 extra points for answering this tough question.

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">

&nbsp;Number of Children controls?&nbsp; &nbsp;

<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>

&nbsp;<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

Robbe Morris replied to Ronald Thomas
04-Nov-09 12:52 PM
Create New Account