问题描述
我创建了一些文本框,我想动态获取它们的值.简而言之,我解释一下我的页面:
I created some textbox and I want to get their value dynamically. Briefly, ı explain my page:
我的下拉列表有数字 1 到 15.当用户选择数字时,我将文本框创建为所选数字.例如;用户选择 3,我创建 3 个文本框,用户在文本框中写一些东西.这是我的代码:aspx 侧:
I have dropDown list has number 1 to 15.When the user select number and I created textbox as selected number. for example; user select 3 and I create 3 text box and user write something in textbox. Here is My Code: 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循环没关系(可以去掉)我的主要目的是获取文本框的值.
Note: for loop doesn't matter (can be removed) My main aim is get the text box value.
如何在 btnSave_Click
方法中获取文本框值?
How to get textbox(es) value in btnSave_Click
method?
推荐答案
下面的代码会帮助你
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 you are used master page then use below code
if (Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString()) != null)
{
var str = Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString());
}
这篇关于如何在代码后面的占位符中获取文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!