private void DataFiller() 改成 public void DataFiller()
同一个页面中,不用public就行
其实写成 void DataFiller() 都行的.
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;
using System.Data .SqlClient;
namespace tiaoshi
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
private void InitializeComponent()
{
this.dg.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dg_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
DataFiller();
}
}
void DataFiller()
{
SqlConnection sqlcon=new SqlConnection("Data Source=localhost;uid=sa;pwd=admin;Initial Catalog=Northwind");
String strSQL="select ProductID,ProductName From Products Order By ProductID";
SqlDataAdapter sqldr=new SqlDataAdapter(strSQL,sqlcon);
DataSet ds=new DataSet();
sqldr.Fill(ds,"dtProducts");
dg.PagerStyle.NextPageText="Next";
dg.PagerStyle.PrevPageText="Previous";
dg.DataSource=ds.Tables["dtProducts"];
dg.DataBind();
sqlcon.Close();
}
private void dg_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dg.CurrentPageIndex=e.NewPageIndex;
DataFiller();
}
}
}
我将程序该成这样,可还是提示这样的错误 CS0122: 不可访问“tiaoshi.WebForm1.dg_PageIndexChanged(object, System.Web.UI.WebControls.DataGridPageChangedEventArgs)”,因为它受保护级别限制
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;
using System.Data .SqlClient;
namespace tiaoshi
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
private void InitializeComponent()
{
this.dg.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dg_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
DataFiller();
}
}
void DataFiller()
{
SqlConnection sqlcon=new SqlConnection("Data Source=localhost;uid=sa;pwd=admin;Initial Catalog=Northwind");
String strSQL="select ProductID,ProductName From Products Order By ProductID";
SqlDataAdapter sqldr=new SqlDataAdapter(strSQL,sqlcon);
DataSet ds=new DataSet();
sqldr.Fill(ds,"dtProducts");
dg.PagerStyle.NextPageText="Next";
dg.PagerStyle.PrevPageText="Previous";
dg.DataSource=ds.Tables["dtProducts"];
dg.DataBind();
sqlcon.Close();
}
private void dg_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dg.CurrentPageIndex=e.NewPageIndex;
DataFiller();
}
}
}
我将程序该成这样,可还是提示这样的错误 CS0122: 不可访问“tiaoshi.WebForm1.dg_PageIndexChanged(object, System.Web.UI.WebControls.DataGridPageChangedEventArgs)”,因为它受保护级别限制
把我标红的那行加上private