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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initgrv();
}
}
protected void Page_Init(object sender, EventArgs e)
{
TemplateField tf3 = new TemplateField(); MyTemplate t3 = new MyTemplate("Del", "删除", "Customer", "Del"); tf3.ItemTemplate = t3; grvTable.Columns.Add(tf3);
} private void Initgrv()
{
SqlDataSource1.ProviderName = "System.Data.OleDb";
SqlDataSource1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString()) + @"\App_Data\ACCESS\temp.mdb";
SqlDataSource1.SelectCommand = "SELECT * FROM Customer"; grvTable.DataSourceID = "SqlDataSource1";
grvTable.AutoGenerateColumns = false;
grvTable.DataKeyNames = new string[] { "CustomerId" };
grvTable.AllowPaging = true;
grvTable.AllowSorting = true;
grvTable.PageSize = 5;
} public class MyTemplate : System.Web.UI.Page, ITemplate
{
private string strText, strType, strFieldId, strFunc; public string strSql = ""; public string strfieldid
{
set
{
strFieldId = value;
}
get
{
return strFieldId;
}
} public MyTemplate()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public MyTemplate(string strType, string strText, string strFieldId, string strFunc)
{
this.strText = strText; this.strType = strType; this.strFieldId = strFieldId; this.strFunc = strFunc;
} public void InstantiateIn(Control container)
{
//删除按钮 LinkButton lkb = new LinkButton(); lkb.DataBinding += new EventHandler(lkb_DataBinding); lkb.Click += new EventHandler(lkb_Click); container.Controls.Add(lkb); } void lkb_Click(object sender, EventArgs e)
{
//操作数据库
LinkButton lkb = (LinkButton)sender; GridViewRow container = (GridViewRow)lkb.NamingContainer; string strId = "aaa";
if (container.DataItem != null)
{
strId = DataBinder.Eval(container.DataItem, "CustomerId").ToString();
} lkb.Text = strId;
} void lkb_DataBinding(object sender, EventArgs e)
{
LinkButton lkb = (LinkButton)sender; GridViewRow container = (GridViewRow)lkb.NamingContainer; lkb.Text = strText;
}
}
}
出错提示:引发了“System.ArgumentNullException”类型的异常
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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initgrv();
}
}
protected void Page_Init(object sender, EventArgs e)
{
TemplateField tf3 = new TemplateField(); MyTemplate t3 = new MyTemplate("Del", "删除", "Customer", "Del"); tf3.ItemTemplate = t3; grvTable.Columns.Add(tf3);
} private void Initgrv()
{
SqlDataSource1.ProviderName = "System.Data.OleDb";
SqlDataSource1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString()) + @"\App_Data\ACCESS\temp.mdb";
SqlDataSource1.SelectCommand = "SELECT * FROM Customer"; grvTable.DataSourceID = "SqlDataSource1";
grvTable.AutoGenerateColumns = false;
grvTable.DataKeyNames = new string[] { "CustomerId" };
grvTable.AllowPaging = true;
grvTable.AllowSorting = true;
grvTable.PageSize = 5;
} public class MyTemplate : System.Web.UI.Page, ITemplate
{
private string strText, strType, strFieldId, strFunc; public string strSql = ""; public string strfieldid
{
set
{
strFieldId = value;
}
get
{
return strFieldId;
}
} public MyTemplate()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public MyTemplate(string strType, string strText, string strFieldId, string strFunc)
{
this.strText = strText; this.strType = strType; this.strFieldId = strFieldId; this.strFunc = strFunc;
} public void InstantiateIn(Control container)
{
//删除按钮 LinkButton lkb = new LinkButton(); lkb.DataBinding += new EventHandler(lkb_DataBinding); lkb.Click += new EventHandler(lkb_Click); container.Controls.Add(lkb); } void lkb_Click(object sender, EventArgs e)
{
//操作数据库
LinkButton lkb = (LinkButton)sender; GridViewRow container = (GridViewRow)lkb.NamingContainer; string strId = "aaa";
if (container.DataItem != null)
{
strId = DataBinder.Eval(container.DataItem, "CustomerId").ToString();
} lkb.Text = strId;
} void lkb_DataBinding(object sender, EventArgs e)
{
LinkButton lkb = (LinkButton)sender; GridViewRow container = (GridViewRow)lkb.NamingContainer; lkb.Text = strText;
}
}
}
出错提示:引发了“System.ArgumentNullException”类型的异常
不知道你执行到哪句话出的错
{
//操作数据库
LinkButton lkb = (LinkButton)sender; GridViewRow container = (GridViewRow)lkb.NamingContainer; string strId = "aaa";
if (container.DataItem != null)
{
strId = DataBinder.Eval(container.DataItem, "CustomerId").ToString();
} lkb.Text = strId;
}
其中container.DataItem为空,为什么会出现这样的错误呢,难道是刷新了?