我有以下用于GridView2的RowDataBound方法
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
List<TextBox> list = new List<TextBox>();
if (ViewState["Table"] != null)
Assessments = (DataTable)ViewState["Table"];
int count = 1;
foreach (DataRow row in Assessments.Rows)
{
TextBox txt = new TextBox();
txt.ID = "AsTxt";
txt.Text = string.Empty;
txt.TextChanged += OnTextChanged;
e.Row.Cells[count].Controls.Add(txt);
count += 2;
listd.Add((e.Row.DataItem as DataRowView).Row[0].ToString() + "Txt");
}
}
}
然后执行以下事件(单击按钮)以检索在GridView的文本框中编写的内容
protected void CalculateBtn_Click(object sender, EventArgs e)
{
GridViewRow rr = GridView2.Rows[0];
TextBox rrrr = (rr.FindControl("AsTxt") as TextBox);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + rrrr.Text + "')", true);
}
我总是得到NullReferenceException。这意味着TextBox对象(rrrr)始终为null。我确定该文本对象位于GridView2.Rows [0]中。
为什么会这样呢?
这是Dynamical
在ASP中创建的控件的已知问题。因此,如果您想在自己的控件上使用创建的控件,postback
那么我建议您在page_int之外声明控件,并在init中进行初始化,然后使用其名称而不是find控件。
查看此博客,这可能对您有帮助
http://techbrij.com/retrieve-value-of-dynamic-controls-in-asp-net
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句