简体   繁体   English

使用C#添加行的Gridview

[英]Gridview with C# adding rows

I want to add a row to my gridview. 我想在网格视图中添加一行。 i succeeded in adding text boxes, but i cannot extract the value It keeps telling me that object reference not set to an instance of an object. 我成功添加了文本框,但无法提取该值。它一直告诉我对象引用未设置为对象的实例。 At this line it halts 在这行停止

string acc = Convert.ToString(((TextBox)GridView1.FooterRow.FindControl("accountID")).Text);

Please what am i doing wrong 请问我在做什么错

  1. You don't need to convert a string to a string( TextBox.Text returns already a string). 您不需要将字符串转换为字符串( TextBox.Text已经返回一个字符串)。

How and where have you added the TextBox to the footer-row? 您如何以及在何处将TextBox添加到页脚行?

i added the TextBox to the footer row in GridView1_RowDataBound 我在GridView1_RowDataBound的页脚行中添加了TextBox

RowDataBound isn't the right method for dynamic controls since it is called only on databinding and not on every postback. RowDataBound不是动态控件的正确方法,因为它仅在数据绑定时调用,而不是在每次回发时调用。 But dynamical controls need to be recreated on every postback. 但是需要在每个回发中重新创建动态控件。

So use RowCreated instead to create controls dynamically and use RowDataBound to databind them. 因此,请使用RowCreated来动态创建控件,并使用RowDataBound进行数据绑定。

protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        TextBox tb = new TextBox();
        tb.ID = "accountID";
        e.Row.Cells[indexOfColumn].Controls.Add(tb);
    }
}

Ok first check wether its a footer row ,then find the textBox in it 好的,首先检查它的页脚行,然后在其中找到文本框

protected void grdAccounts_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.Footer)
  {
   //get text box value here
  }
}

In Button Click 在按钮中单击

try this 尝试这个

GridViewRow row = GridView1.FooterRow; 
firstName = ((TextBox)row.FindControl("TextBox1")).Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM