我创造了:
>一个母版页和一个名为Detail的内容页.
>按钮单击事件,在网格视图中显示数据.
>在网格视图中,列是自动生成的.
>我想在网格视图中显示11列,但它不仅仅是页面
尺寸.
该怎么办?
我已经创建了数据库连接代码的sql助手文件,并调用该方法,而不是使用sqldatasource进行连接.
The GridView ‘GridView1’ fired event PageIndexChanging which wasn’t
handled.
解决方法
您需要在代码后面声明一个处理PageIndexChanging事件的方法.
类似的东西:
protected void GridView1_PageIndexChanging (object sender,GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bindGridView(); //bindgridview will get the data source and bind it again } private void bindGridView() { GridView1.DataSource=getData(); GridView1.DataBind(); }
提供示例代码:
protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bindGridView(); //bindgridview will get the data source and bind it again } protected void Page_Load(object sender,EventArgs e) { if(!IsPostBack) bindGridView(); } //this is some sample data private void bindGridView() { DataTable t = new DataTable(); t.Columns.Add("Col1"); t.Columns.Add("Col2"); DataRow r = null; for (int i = 0; i < 25; i++) { r = t.NewRow(); r.ItemArray = new object[] { "Val" + i," Another " + i }; t.Rows.Add(r); } GridView1.DataSource = t; GridView1.DataBind(); }
这是标价:
<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">
产生这个: