[英]get dynamically generated ajax textbox id
I have created a web application in which I need to generate textboxes depending upon the number the user enters.I have used Ajax and created textboxes dynamically but these textboxes are not accessible from code-behind.I have used find control
but to no use.This is the code I use to generate textbox. 我创建了一个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);
}
}
}
You can access the element by name using Request.Form["NameOfFormControl"] You will have to make little change in code. 您可以使用Request.Form [“ NameOfFormControl”] 通过名称访问元素。您将几乎不需要更改代码。
In javascript 在JavaScript中
txt.Name = "txtbranchloc" + i.ToString();
In Code Behind 在背后的代码中
Note: Make sure you access it in postback otherwise you get null 注意:请确保您以回发方式访问它,否则将返回null
if (Page.IsPostBack)
{
string firstTxtVal = Request.Form["txtbranchloc0"].ToString();
string secondTxtVal = Request.Form["txtbranchloc1"].ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.