我创建了一些文本框,我希望动态获取它们的值。 简而言之,ı解释我的页面:
我有dropDown列表的编号为1到15.当用户选择编号时,我创建了文本框作为选中的编号。 例如;用户选择3,我创建3个文本框,用户在文本框中写入内容。 这是我的代码: aspx Side:
<asp:DropDownList ID="ddlUserSelected" AutoPostBack="true" OnSelectedIndexChanged="ddlUserSelected_SelectedIndexChanged" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
代码背后:
protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
TextBox txtDate = new TextBox();
Label lbl = new Label();
lbl.Text = "<br/>";
txtDate .Width = 70;
txtDate .CssClass = "tbl";
txtDate .ID = "txtDate" + a;
PlaceHolder1.Controls.Add(txtDate);
PlaceHolder1.Controls.Add(lbl);
}
}
此外,我还有保存按钮。
protected void btnSave_Click(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
//I want to get each textbox value
}
}
注意:for循环无关紧要(可以删除)我的主要目的是获取文本框值。
如何在btnSave_Click
方法中获取文本框值?
答案 0 :(得分:2)
以下代码可以帮助您
protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
TextBox txtDate = new TextBox();
Label lbl = new Label();
lbl.Text = "<br/>";
txtDate.Width = 70;
txtDate.CssClass = "tbl";
txtDate.ID = "txtDate" + a;
PlaceHolder1.Controls.Add(txtDate);
PlaceHolder1.Controls.Add(lbl);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
if(Request.Form.Get("txtDate" + a.ToString()) != null)
{
var str = Request.Form.Get("txtDate" + a.ToString());
}
}
}
如果您使用的是母版页,请使用以下代码
if (Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString()) != null)
{
var str = Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString());
}
答案 1 :(得分:0)
protected void btnSave_Click(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
string value = Request.Form["txtDate" + a];
}
}
答案 2 :(得分:0)
如果您动态地向页面添加控件,则需要在 Page Init 或页面加载上重新加载它们。
否则,当您回发时,您将无法找到它们。
<asp:DropDownList ID="ddlUserSelected" AutoPostBack="true"
OnSelectedIndexChanged="ddlUserSelected_SelectedIndexChanged"
runat="server">
<asp:ListItem Text="Select one" />
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<asp:ListItem Text="4" />
<asp:ListItem Text="5" />
</asp:DropDownList>
<br/>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"/>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
private int Total
{
get
{
int total;
if (Int32.TryParse(ddlUserSelected.SelectedItem.Text, out total))
return total;
return 0;
}
}
protected void Page_Load(object sender, EventArgs e)
{
CreateTextBoxes(Total);
}
protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
{
CreateTextBoxes(Total);
}
protected void btnSave_Click(object sender, EventArgs e)
{
int total = Total;
for (int a = 1; a <= total; a++)
{
var textbox = PlaceHolder1.FindControl("txtDate" + a) as TextBox;
}
}
private void CreateTextBoxes(int total)
{
for (int a = 1; a <= total; a++)
{
// Make sure we do not add same ID again
if (PlaceHolder1.FindControl("txtDate" + a) == null)
{
TextBox txtDate = new TextBox();
Label lbl = new Label();
lbl.Text = "<br/>";
txtDate.Width = 70;
txtDate.CssClass = "tbl";
txtDate.ID = "txtDate" + a;
PlaceHolder1.Controls.Add(txtDate);
PlaceHolder1.Controls.Add(lbl);
}
}
}