日期:2014-05-18  浏览次数:20633 次

在datagrid中实现批量删除时出现:未将对象引用设置到对象的实例
弄了一个通宵还是没搞好
以下是代码,当我点checkbox并点击删除按钮的时候。出现错误提示:未将对象引用设置到对象的实例
以下是代码
CS:
C# code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CHINAESL.News
{
    /// <summary>
    /// Manage 的摘要说明。
    /// </summary>
    public partial class Manage : System.Web.UI.Page
    {

        public DataSet ds=new DataSet ();//将DataSet实例化
                
        protected void Page_Load(object sender, System.EventArgs e)
        {    
            if(Session["UserName"].ToString().Equals("worldlink"))
            {
                ds = Socut.Data.ExecuteDataSet("SELECT * FROM socut_news ORDER BY n_time DESC","TableName");
                DataGrid1.DataSource =ds;//设置数据源
                DataGrid1.DataBind ();//绑定数据    
            }
            else
            {                           
                Response.Write("<script language=javascript>alert('此页面无效,请返回!')</script>");
                Response.Write("<script language=javascript>window.location.href='/CHINAESL/default.aspx'</script>");            
            }
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

        }
        #endregion

        private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            DataBind(); 
        }


        protected void LinkButton1_OnClick(object sender, EventArgs e)
        {
            {
                for (int i = 0; i < DataGrid1.Items.Count; i++)
                {
                    CheckBox mychk = (CheckBox)DataGrid1.Items[i].FindControl("Item");
                    if (mychk.Checked == true)
                    {
                        string myid = Request.Form["Item"];
                        Socut.Data.ExecuteDataSet("DELETE FROM socut_news WHERE n_id='" + myid + "'");
                    }
                }

            }
        }
}
}


aspx:
HTML code
<%@ Page language="c#" Inherits="CHINAESL.News.Manage" CodeFile="Manage.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>Manage</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
<script type="text/javascript">
    function SelectAll(tempControl)
    {
      var theBox=tempControl;