gridview更新时出现未将对象的引用设置到对象的实例

时间:2022-10-30 14:48:29
问题如上
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EMangement : System.Web.UI.Page
{
    private DataSet ds;
    RockmanYl.BLL.Employees getEmployeeList = new RockmanYl.BLL.Employees();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["EmployeeName"] == null)
        {
            Response.Redirect("ELogin.aspx");
        }
        else
        {
            RockmanYl.DBUtility.CheckPower.HasFunctionInModule(this, "人事管理", "Access", "NPower.aspx");
            Bind("");

            if (!IsPostBack)
            {
                DropDownList_Employee.Items.Clear();
                for (int i = 1; i <= GridView_Employee.PageCount; i++)
                {
                    DropDownList_Employee.Items.Add(i.ToString());
                }
            }
        }
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="Text"></param>
    private void Bind(string Text)
    {
        BindData(Text);
        GridView_Employee.DataBind();
    }
    private void BindData(string Text)
    {
        ds = getEmployeeList.GetAllList();
        GridView_Employee.DataSource = ds;
        GridView_Employee.DataKeyNames = new string[] { "EmployeeId" };
    }
   
    protected void GridView_Employee_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_Employee.PageIndex = e.NewPageIndex;
        Bind("");
    }
    protected void DropDownList_Employee_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView_Employee.PageIndex = Convert.ToInt32(DropDownList_Employee.SelectedValue) - 1;
        Bind("");
    }
    protected void GridView_Employee_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView_Employee.EditIndex = e.NewEditIndex;
        Bind("");
    }

    protected void GridView_Employee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView_Employee.EditIndex = -1;
        Bind("");
    }
     protected void GridView_Employee_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(GridView_Employee.DataKeys[e.RowIndex].Values[0].ToString());
        string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text;
        string password = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[2].FindControl("Password")).Text;
        string question = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[3].FindControl("Question")).Text;
        string answer = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[4].FindControl("Answer")).Text;
        int roleID = Convert.ToInt32(((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[5].FindControl("RoleId")).Text);
        RockmanYl.BLL.Employees updateEmployee = new RockmanYl.BLL.Employees();
        RockmanYl.Model.Employees employee = new RockmanYl.Model.Employees();
        employee.EmployeeId = ID;
        employee.EmployeeName = employeeName;
        employee.Password = password;
        employee.Question = question;
        employee.Answer = answer;
        employee.RoleId = roleID;
        updateEmployee.Update(employee);
        Bind("");
        

    }
}

11 个解决方案

#1


设断点调试下,看看具体哪行未引用对象

#2


 调试了, string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text; 这一行就出现了这个问题

#3


估计是GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")没找到控件,发前台html代码看看,GridView_Employee的代码

#4


先判断一下:
if (GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName") != null)
{
   string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text; 
}

#5


%@ Page Language="C#" AutoEventWireup="true" CodeFile="EManagement.aspx.cs" Inherits="EMangement" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
   <div>
        <asp:GridView ID="GridView_Employee" runat="server" PageSize="5" OnPageIndexChanging="GridView_Employee_PageIndexChanging" OnRowCancelingEdit="GridView_Employee_RowCancelingEdit" OnRowDeleting="GridView_Employee_RowDeleting" OnRowEditing="GridView_Employee_RowEditing" Width="553px" OnRowUpdating="GridView_Employee_RowUpdating"  >
      

            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                            Text="更新"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="取消"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="编辑"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                   <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('确认要删除吗?')"
                            Text="删除"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;<asp:DropDownList ID="DropDownList_Employee" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_Employee_SelectedIndexChanged" >
        </asp:DropDownList>
         <a href="ERegister.aspx" class="darkb">添加</a></div>
    </form>
</body>
</html>


#6


楼主可以先看下生成的HTML页面中你想要的TextBox的ID是不是你写的那个。

#7


我不是很擅长用findcontrol,findcontrol(),括号中的值不是gridview中要更新的列名而是一个textbox的ID,那怎样将这个textbox与gridview联系起来?

#8


未见到你的界面上放有ID为EmployeeName、Password、Question、Answer、RoleId的控件嘛,必须是控件的ID是这些名字,才能使用FindControl()方法,还有,我看你这个数据列似乎是用了绑定列,自动生成的吧,取绑定列编辑状态值可以这样:

//假设你的"员工姓名"是第三列,后面的依次排列,则列下标从2开始,我看了下,你第0列是编辑、第1列是删除、后面的数据至少从第2列开始吧
string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
string password = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
string question = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
string answer = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[5].Controls[0]).Text; 
int roleID = Convert.ToInt32(((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[6].Controls[0]).Text); 

#9


楼主我在你的HTML代码GridView中就没有看到一个TextBox,请问没有TextBox你怎么能找到TextBox呢

#10


该回复于2009-07-10 16:41:59被版主删除

#11


        string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text; 
        string password = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[2].FindControl("Password")).Text; 
        string question = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[3].FindControl("Question")).Text; 
        string answer = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[4].FindControl("Answer")).Text; 
        int roleID = Convert.ToInt32(((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[5].FindControl("RoleId")).Text); 
在源码看看有没这些控件

#1


设断点调试下,看看具体哪行未引用对象

#2


 调试了, string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text; 这一行就出现了这个问题

#3


估计是GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")没找到控件,发前台html代码看看,GridView_Employee的代码

#4


先判断一下:
if (GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName") != null)
{
   string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text; 
}

#5


%@ Page Language="C#" AutoEventWireup="true" CodeFile="EManagement.aspx.cs" Inherits="EMangement" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
   <div>
        <asp:GridView ID="GridView_Employee" runat="server" PageSize="5" OnPageIndexChanging="GridView_Employee_PageIndexChanging" OnRowCancelingEdit="GridView_Employee_RowCancelingEdit" OnRowDeleting="GridView_Employee_RowDeleting" OnRowEditing="GridView_Employee_RowEditing" Width="553px" OnRowUpdating="GridView_Employee_RowUpdating"  >
      

            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                            Text="更新"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="取消"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="编辑"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                   <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('确认要删除吗?')"
                            Text="删除"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;<asp:DropDownList ID="DropDownList_Employee" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_Employee_SelectedIndexChanged" >
        </asp:DropDownList>
         <a href="ERegister.aspx" class="darkb">添加</a></div>
    </form>
</body>
</html>


#6


楼主可以先看下生成的HTML页面中你想要的TextBox的ID是不是你写的那个。

#7


我不是很擅长用findcontrol,findcontrol(),括号中的值不是gridview中要更新的列名而是一个textbox的ID,那怎样将这个textbox与gridview联系起来?

#8


未见到你的界面上放有ID为EmployeeName、Password、Question、Answer、RoleId的控件嘛,必须是控件的ID是这些名字,才能使用FindControl()方法,还有,我看你这个数据列似乎是用了绑定列,自动生成的吧,取绑定列编辑状态值可以这样:

//假设你的"员工姓名"是第三列,后面的依次排列,则列下标从2开始,我看了下,你第0列是编辑、第1列是删除、后面的数据至少从第2列开始吧
string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
string password = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
string question = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
string answer = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[5].Controls[0]).Text; 
int roleID = Convert.ToInt32(((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[6].Controls[0]).Text); 

#9


楼主我在你的HTML代码GridView中就没有看到一个TextBox,请问没有TextBox你怎么能找到TextBox呢

#10


该回复于2009-07-10 16:41:59被版主删除

#11


        string employeeName = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[1].FindControl("EmployeeName")).Text; 
        string password = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[2].FindControl("Password")).Text; 
        string question = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[3].FindControl("Question")).Text; 
        string answer = ((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[4].FindControl("Answer")).Text; 
        int roleID = Convert.ToInt32(((TextBox)GridView_Employee.Rows[e.RowIndex].Cells[5].FindControl("RoleId")).Text); 
在源码看看有没这些控件