下拉框选择显示C#中的文本框
本文关键字:文本 显示 选择 | 更新日期: 2023-09-27 17:59:55
<div class="row500">
<span class="cell200">
<asp:DropDownList ID="ddlCollision" runat="server" Height="28px" Width="354px" Style="font-size: 18px" OnSelectedIndexChanged="ddlCollision_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1">Cars</asp:ListItem>
<asp:ListItem Value="2">People</asp:ListItem>
</asp:DropDownList>
</span>
</div>
<br/>
<div class="row500" id="fixedObject" runat="server">
<span class="cell200">
<asp:Label ID="lblFixed" runat="server" Text="Car Label:"> </asp:Label>
</span>
<br />
<span class="cell200">
<asp:TextBox ID="TextBox9" runat="server"> </asp:TextBox>
</span>
</div>
我目前正在开发一个asp.net C#网页,该网页包含一个下拉列表,其中大约有5个列表项,值从0-4不等。每次用户选择其中一个项目时,我都希望数字与将出现的文本框的数量相对应。因此,如果用户从下拉列表中选择"2",我需要显示/隐藏包含文本框的div,并使2个文本框出现。。任何帮助都将是伟大的!!!!
我的尝试:下拉列表下的代码
protected void ddlCollision_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlCollision.SelectedValue == "1")
fixedObject.Visible = true;
else
fixedObject.Visible = false;
}
fixed object是包含文本框的div的id
您甚至可以根据从下拉列表中选择的整数动态添加文本框。然后,对于count
的次数,它将运行并添加那么多文本框。
protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e)
{
for (int loopcnt = 1; loopcnt <= Convert.ToInt32(ddlOptions.SelectedValue.Trim()); loopcnt++)
{
TextBox tb = new TextBox();
tb.ID = "tb" + loopcnt;
ctrlPlaceholder.Controls.Add(tb);
}
}
<div>
Question:
<asp:TextBox ID="txtQuestion" runat="server"></asp:TextBox>
<br />
<br />Number of Options:
<asp:DropDownList ID="ddlOptions" runat="server" AutoPostBack="true" onselectedindexchanged="ddlOptions_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
<br />
<br />Options:
<asp:PlaceHolder runat="server" ID="ctrlPlaceholder"></asp:PlaceHolder>
</div>
protected void ddlOptions_SelectedIndexChanged(对象发送方,EventArgs e){
Int32 pickedValue = Int32.Parse(ddlOptions.SelectedValue);
Table table = new Table();
for (int i = 1; i <= pickedValue; i++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Attributes.Add("runat", "server");
TextBox txt_splzn = new TextBox();
txt_splzn.ID = "txtB" + i.ToString();
txt_splzn.Text = "Text Number " + i.ToString();
cell.Controls.Add(txt_splzn);
row.Cells.Add(cell);
table.Rows.Add(row);
ctrlPlaceholder.Controls.Add(table);
}
}
}