datagrid修改问题
default.aspx.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
void Page_Load(object sender, EventArgs e)
{
// OleDbConnection acon = new OleDbConnection( "Provider=Microsoft.Jet.OleDb.4.0;Data Source=G:\\vs2005\\easynews1\\App_Data\\db.mdb ");
// DataSet ds = new DataSet();
// acon.Open();
// OleDbDataAdapter access = new OleDbDataAdapter( "Select top 6 * from news ", acon);
// access.Fill(ds, "copynews ");
// dg.DataSource = ds.Tables[ "copynews "].DefaultView;
// dg.DataBind();
// acon.Close();
// acon = null;
if (!Page.IsPostBack) bindgrid();
}
void bindgrid()
{
OleDbConnection acon = new OleDbConnection( "Provider=Microsoft.Jet.OleDb.4.0;Data Source=G:\\vs2005\\easynews1\\App_Data\\db.mdb ");
DataSet ds = new DataSet();
acon.Open();
OleDbDataAdapter access = new OleDbDataAdapter( "Select top 6 * from news ", acon);
access.Fill(ds, "copynews ");
dg.DataSource = ds.Tables[ "copynews "].DefaultView;
dg.DataBind();
acon.Close();
acon = null;
}
void gridedit(object sender, DataGridCommandEventArgs e)
{
dg.EditItemIndex = e.Item.ItemIndex;
bindgrid();
}
void gridcancel(object sender, DataGridCommandEventArgs e)
{
dg.EditItemIndex = -1;
bindgrid();
}
void gridupdate(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string newsid, strsql, content;
int introw;
TextBox obj;
introw = e.Item.ItemIndex;
obj = (TextBox)dg.Items[introw].Cells[2].Controls[0];
content = obj.Text;
newsid = dg.DataKeys[introw].ToString();
strsql = "update news set news_content= ' " + content + " 'where news_id= ' " + newsid + " ' ";
OleDbConnection acon = new OleDbConnection( "Provider=Microsoft.Jet.OleDb.4.0;Data Source=G:\\vs2005\\easynews1\\App_Data\\db.mdb ");
acon.Open();
OleDbCommand acom = new OleDbCommand();
acom.Connection = acon;
acom.CommandText = strsql;
acon.Close();
acon = null;
acom = null;
dg.EditItemIndex = -1;
bindgrid();
acon.Close();
acon = null;
}
}
default.aspx
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
<!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:datagrid id= "dg " runat= "server " cellspacing= "1 " AutoGenerateColumns= "false " OnEditCommand= "gridupdate " DataKeyField= "news_id ">
<columns>
<asp:BoundColumn datafield= "news_title " headertext= "新闻标题 " ReadOnly= "true "/>
<asp:BoundColumn datafield= "news_content " headertext= "新闻内容 "/>
<asp:editcommandcolumn HeaderText= "操作 " UpdateText= "修改 "/>
</columns>
</asp:datagrid> <br/>
</div>
</form>
</body>
</html>
数据库表news
news_id news_title news_content
程序出现错误
“_Default.gridupdate(object, System.Web.UI.WebControls.DataGridCommandEventArgs)”不可访问,因为它受保护级别限制
高手能帮帮看看吗
[解决办法]
以下这些方法前面加上 protected, 如
protected void gridedit(object sender, DataGridCommandEventArgs e)
protected void gridcancel(object sender, DataGridCommandEventArgs e)
protected void gridupdate(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)