| Need to dynamically create controls base on dropdownlist selection |
| Ronald Thomas posted at Tuesday, November 03, 2009 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;
}
}
}
|
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| November |
0 |
$0.00 |
0 |
| October |
0 |
$0.00 |
0 |
|
|
|
|
|
|
|
|