[英]Cannot read value from dynamically created textbox
I want to retrieve a textbox value in a button click event, but as soon as the button is clicked, the postback fires and the value is empty. 我想在按钮单击事件中检索文本框值,但是一旦单击按钮,就会触发回发并且该值为空。 I have tried to create the textbox in a (
!isPostBack
) but that doesn't seem to work. 我试图在(
!isPostBack
)中创建文本框,但这似乎不起作用。
protected void Page_Load(object sender, EventArgs e)
{
Form.Controls.Add(t);
}
protected void Page_PreInit(object sender, EventArgs e)
{
predictionList = dc.getPredictions(Convert.ToInt32(Session["accountId"]));
fixtureList = dc.getFixtures();
t.CssClass = "panel panel-success table table-striped";
sortLists();
foreach (Fixture f in newList)
{
TableRow tr = new TableRow();
TableCell tc1= new TableCell();
TextBox tb1= new TextBox();
tb1.ID = "tb1";
tc1.Controls.Add(tb1);
tr.Cells.Add(tc1);
t.Rows.Add(tr);
}
}
Here I add the control, and here I want to process whatever is in the textbox: 在这里添加控件,在这里我要处理文本框中的内容:
protected void btSubmit_Click(object sender, EventArgs e)
{
foreach (TableRow r in t.Rows)
{
string textboxRead= ((TextBox)r.FindControl("tb1")).text;
int textboxInt = Convert.ToInt32(textboxRead);
}
}
Could it be that FindControl()
is not finding the textbox because it doesn't work recursively? 难道是因为
FindControl()
不能递归地工作,所以找不到文本框?
ie You have added the TextBox to a TableCell
, but you are performing your FindControl()
call on the TableRow
, not the TableCell
. 即您已经将TextBox添加到
TableCell
,但是您正在TableRow
而不是TableCell
上执行FindControl()
调用。 So either call FindControl() from the Cell or use a recursive version. 因此,可以从Cell调用FindControl()或使用递归版本。
For a recursive version of FindControl(), see: Better way to find control in ASP.NET 有关FindControl()的递归版本,请参见: 在ASP.NET中查找控件的更好方法
Try this: 尝试这个:
TextBox tb1;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
tb1 = ((TextBox)r.FindControl("tb1"));
}
}
protected void btSubmit_Click(object sender, EventArgs e)
{
string textboxRead = tb1.Text; // here you can get the tb1.Text
int textboxInt = Convert.ToInt32(textboxRead);
}
Hope this will help you. 希望这会帮助你。
protected void AddTextBox(object sender, EventArgs e)
{
int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtDynamic" + index);
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
pnlTextBoxes.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
see this link for details: http://www.aspsnippets.com/Articles/Get-Value-Text-of-dynamically-created-TextBox-in-ASPNet-using-C-and-VBNet.aspx 有关详细信息,请参见此链接: http : //www.aspsnippets.com/Articles/Get-Value-Text-of-dynamically-created-TextBox-in-ASPNet-using-C-and-VBNet.aspx
You need to give the ID's to all of your dynamically created controls. 您需要为所有动态创建的控件提供ID。 This is mandatory to prevent any ambiguity on post-back.
这是强制性的,以防止在回发时产生任何歧义。
That includes tr
, tc1
, tb1
and maybe even t
controls. 其中包括
tr
, tc1
, tb1
甚至可能还有t
控件。
Also, to find the value, use this snippet: 另外,要查找值,请使用以下代码段:
protected void btSubmit_Click(object sender, EventArgs e)
{
foreach (TableRow tr in t.Rows)
{
var tc1 = (TableCell)tr.FindControl("tc1");
var tb1 = (TextBox)tc1.FindControl("tb1");
int textboxInt = Convert.ToInt32(tb1.Text);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.