编辑gridview时,对象设置为Null Reference

时间:2015-11-26 04:02:34

标签: c# asp.net gridview

在我的Web应用程序中,我有一个网格视图,这个网格视图是数据绑定的。在这个网格视图上方,我有一个文本框,用户可以在其中输入员工ID并在网格视图中显示员工姓名。在网格视图中,它具有编辑图标。当用户点击该编辑图标时,将出现网格视图中的一些文本框,并且用户可以编辑文本框中的文本。我到目前为止工作了。但是,我的问题是当我尝试编辑网格视图中的空文本框时,它给了我这个错误:

  

对象未设置为对象的实例

我所做的就是检查文本框是否为空。这确实忽略了错误,但实际上并不是我想要的

protected void txtEditedEmployeeID_TextChanged(object sender, EventArgs e)
{
    DatabaseManager dbManager = Common.GetDbManager();
    foreach (GridViewRow row in gridViewAddEmployee.Rows)
    {
        TextBox txtEmployee= (TextBox)row.FindControl("txtEmployeeID");
        if (txtEmployee!= null)
        {
            string personID = txtEmployee.Text;
            DataSet dsRE = dbManager.GetEmployeeNameByID(personID);
            for (int i = 0; i < dsRE.Tables[0].Rows.Count; i++)
            {
                string employeeFirstName = dsRE.Tables[0].Rows[i]["FIRST_NAME"].ToString();
                string employeeLastName = dsRE.Tables[0].Rows[i]["LAST_NAME"].ToString();
                ((TextBox)row.FindControl("txtEmployeeName")).Text = staffLastName + " " + staffFirstName;
            }
        }
        else
        {
        }
    }
}

HTML ASP.NET代码

   <asp:TemplateField HeaderText="EmployeeID">
              <EditItemTemplate>
                    <asp:TextBox ID="txtEmployeeID" runat="server"  AutoPostBack="true" OnTextChanged= "txtEditedEmployeeID_TextChanged" Text='<%#Bind("Employee_ID") %>'Width="90px"></asp:TextBox>
              </EditItemTemplate>

              <ItemTemplate>
                   <asp:Label ID="lblEmployeeID" runat="server"  CssClass="GridInput" Text='<%#Bind("Employee_ID") %>'Width="90px"></asp:Label>
              </ItemTemplate>
    </asp:TemplateField>

<asp:TemplateField HeaderText="Employee Name">
      <EditItemTemplate>
           <asp:TextBox ID="txtEmployee" runat="server" ReadOnly ="true" Text='<%#Bind("Full_Name") %>'Width="90px"></asp:TextBox>
       </EditItemTemplate>
       <ItemTemplate>
           <asp:Label ID="lblEmployeeName" runat="server" CssClass="GridInput" Text='<%#Bind("Full_Name") %>'Width="90px"></asp:Label>
       </ItemTemplate>
</asp:TemplateField>

2 个答案:

答案 0 :(得分:1)

正如编辑txtEmployeeID将仅针对该行显示并非针对GridView的所有行以及TextBox更改事件,您正在查找所有行编辑的TextBox,直到现在才显示,因此您可以尝试这样:

  protected void txtEditedEmployeeID_TextChanged(object sender, EventArgs e)
  {
      DatabaseManager dbManager = Common.GetDbManager();
      TextBox txt = sender as TextBox; // Edited TextBox
      GridViewRow row = (txt.NamingContainer as GridViewRow);// Row of Edited TextBox
      TextBox txtEmployee= (TextBox)row.FindControl("txtEmployeeID");
      string personID = txtEmployee.Text;
      DataSet dsRE = dbManager.GetEmployeeNameByID(personID);
      for (int i = 0; i < dsRE.Tables[0].Rows.Count; i++)
        {
          string employeeFirstName = dsRE.Tables[0].Rows[i]["FIRST_NAME"].ToString();
          string employeeLastName = dsRE.Tables[0].Rows[i]["LAST_NAME"].ToString();
          ((TextBox)row.FindControl("txtEmployeeName")).Text = staffLastName + " " + staffFirstName;
        }
  }

答案 1 :(得分:0)

试试这个:

而不是

TextBox txtEmployee= (TextBox)row.FindControl("txtEmployeeID");
if (txtEmployee!= null)
{
    string personID = txtEmployee.Text;

你应该像这样写

TextBox txtEmployee = new TextBox();
txtEmployee = (TextBox)row.FindControl("txtEmployeeID"); 
if (!string.isNullorEmpty(txtEmployee.Text))
{
    string personID = txtEmployee.Text;