我想创建5个在下拉选择的索引更改时创建的文本框,如果下拉索引是4,则创建4个文本框。下拉列表位于asp.net updatepanel中。内部更新面板中还有一个按钮。当用户点击该按钮时,无论在文本框中输入什么内容,都会显示在标签上。
请建议我解决。
答案 0 :(得分:0)
你可以试试这个......
for (int i = 0; i < ddlTest.SelectedIndex; i++)
{
TextBox txt = new TextBox();
txt.ID = "txt" + i.ToString();
PlaceHolder1.Controls.Add(txt);
}
将ddlTest.SelectedIndex更改为循环条件.....
答案 1 :(得分:0)
她的解决方案是在updatepanel内创建txtbox
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False">
<asp:ListItem Value="1">one </asp:ListItem><asp:ListItem Value="2">Two </asp:ListItem><asp:ListItem Value="3">Three </asp:ListItem><asp:ListItem Value="4">Four</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
protected void Button1_Click(object sender, EventArgs e)
{
int noOftextbox =Convert.ToInt32(DropDownList1.SelectedItem.Value);
for (int i = 0; i < noOftextbox; i++)
{
TextBox txtbox = new TextBox();
txtbox.ID = i.ToString();
txtbox.Visible = true;
txtbox.Height = 30;
txtbox.Width = 100;
txtbox.Text = "txt" + i.ToString();
PlaceHolder1.Controls.Add(txtbox);
//UpdatePanel1.Controls.Add(txtbox);
//UpdatePanel1.Controls.Add(new LiteralControl("<br/>"));
}
}
答案 2 :(得分:0)
你可以试试这个......
//Add Control into Panel
for (int i = 0; i < 5; i++)
{
TextBox txt = new TextBox();
txt.ID = "txt" + i.ToString();
txt.Text = "this is text:" + i.ToString();
pnlTest.Controls.Add(txt);
}
//Fetch Value from Panel
for (int i = 0; i < pnlTest.Controls.Count; i++)
{
string val = ((TextBox)pnlTest.Controls[i]).Text;
}
第一个循环用于向面板添加动态控制,另一个循环用于从控制中获取值....
答案 3 :(得分:0)
愿这项工作
<div>
</div><asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"><asp:ListItem Value="1">one </asp:ListItem><asp:ListItem Value="2">Two </asp:ListItem><asp:ListItem Value="3">Three </asp:ListItem><asp:ListItem Value="4">Four</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button2" runat="server" Text="fectchText" onclick="Button2_Click" />
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (ViewState["mode"].ToString() == "1")
createTextbox();
ViewState.Add("mode", "1");
}
else
{
ViewState.Add("mode", "0");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
createTextbox();
}
public void createTextbox()
{
int noOftextbox = Convert.ToInt32(DropDownList1.SelectedItem.Value);
for (int i = 0; i < noOftextbox; i++)
{
TextBox txtbox = new TextBox();
txtbox.ID = "txt" + i;
txtbox.Visible = true;
txtbox.Height = 30;
txtbox.Width = 100;
UpdatePanel1.ContentTemplateContainer.Controls.Add(txtbox);
PlaceHolder1.Controls.Add(txtbox);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int nocount= Convert.ToInt32(DropDownList1.SelectedItem.Value);
for (int count = 0; count <= nocount-1 ; count++)
{
TextBox txtRead = (TextBox)PlaceHolder1.FindControl("txt" + (count));
Label1.Text += txtRead.Text+"-";
}
}