[英]Get the ID of the dynamically generated textbox which is calling a javascript function
[英]get dynamically generated ajax textbox id
我创建了一个Web应用程序,其中需要根据用户输入的数字生成文本框。我使用了Ajax并动态创建了文本框,但是这些文本框无法从背后的代码中访问。我使用了find control
但没有用。这是我用来生成文本框的代码。
if (txtnobranches.Text != "")
{
if (Convert.ToInt32(txtnobranches.Text) != 0)
{
div_br.InnerHtml = "<table></table>";
tbl_br.Controls.Clear();
for (int i = 0; i < Convert.ToInt32(txtnobranches.Text); i++)
{
TableRow rowbr = new TableRow();
TableCell cellname = new TableCell();
cellname.Text = "Branch Location " + i.ToString();
rowbr.Cells.Add(cellname);
TableCell cellvalue = new TableCell();
cellvalue.Text = "";
TextBox txt = new TextBox();
txt.Text = "";
txt.ID = "txtbranchloc" + i.ToString();
cellvalue.Controls.Add(txt);
cellvalue.ID = "txtbranchloc" + i.ToString();
rowbr.Cells.Add(cellvalue);
tbl_br.Rows.Add(rowbr);
}
}
}
您可以使用Request.Form [“ NameOfFormControl”] 通过名称访问元素。您将几乎不需要更改代码。
在JavaScript中
txt.Name = "txtbranchloc" + i.ToString();
在背后的代码中
注意:请确保您以回发方式访问它,否则将返回null
if (Page.IsPostBack)
{
string firstTxtVal = Request.Form["txtbranchloc0"].ToString();
string secondTxtVal = Request.Form["txtbranchloc1"].ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.