我的问题是:
我试验时是每页显示两行数据,当运行时,第一页显示没有问题,但是我当点击第二页时,页面成了空白页,
没有任何数据。这是哪里出问题了呢?
这里是后台实现分页的函数
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
然后下面是前台部分代码
1 <asp:GridView ID="GridView1" runat="server"
2 AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF"
3 BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
4 onrowcancelingedit="GridView1_RowCancelingEdit1"
5 onrowediting="GridView1_RowEditing1" onrowupdating="GridView1_RowUpdating"
6 onrowdeleting="GridView1_RowDeleting" AllowSorting="True"
7 Font-Size="Medium" Font-Names="华文新魏" PageSize="2"
8 style="margin-right: 108px" onrowdatabound="GridView1_RowDataBound"
9 onsorting="GridView1_Sorting" AllowPaging="True"
10 EnableSortingAndPagingCallbacks="True"
11 onpageindexchanging="GridView1_PageIndexChanging" ViewStateMode="Enabled"
12 >
13 <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" Height="50px"
14 HorizontalAlign="Center" />
15 <Columns>
16 <asp:TemplateField>
17 <ItemTemplate>
18 <asp:CheckBox ID="CheckBox1" runat="server" />
19 </ItemTemplate>
20 </asp:TemplateField>
21 <asp:BoundField HeaderText="编号" />
22 <asp:BoundField DataField="username" HeaderText="用户名" />
23 <asp:BoundField DataField="sex" HeaderText="性别" />
24 <asp:BoundField DataField="age" HeaderText="年龄" />
25 <asp:BoundField DataField="email" HeaderText="邮箱" />
26 <asp:BoundField DataField="telephone" HeaderText="联系方式" />
27 <asp:BoundField DataField="question" HeaderText="保密问题" />
28 <asp:BoundField DataField="answer" HeaderText="答案" />
29 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
30 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
31
32
33 <asp:TemplateField HeaderText="个人照">
34 <ItemTemplate>
35 <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("photo") %>' width="120px" Height="110px"/>
36 </ItemTemplate>
37 <EditItemTemplate>
38 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("photo") %>'></asp:TextBox>
39 </EditItemTemplate>
40 <HeaderStyle Font-Size="Larger" ForeColor="White" />
41 <ItemStyle Font-Size="Larger" />
42 </asp:TemplateField>
43
44
45
46
47 </Columns>
48 <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
49 <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
50 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
51 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
52 <AlternatingRowStyle BackColor="#F7F7F7" />
53 </asp:GridView>
你的数据源是怎么绑定的,是后台写代码绑定的还是直接用datasource控件
是后台写的 也就是这里15行处
1 private void gridview()
2 {
3 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
4 con.Open();
5 string sql = "select * from register";
6 SqlDataAdapter da = new SqlDataAdapter(sql, con);
7 DataSet ds = new DataSet();
8 da.Fill(ds,"tb_register");
9 GridView1.AllowPaging = true;
10 GridView1.GridLines = GridLines.Both;
11 GridView1.EnableSortingAndPagingCallbacks = false;
12 GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;
13 GridView1.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
14 GridView1.PageSize = 2;
15 GridView1.DataSource = ds.Tables[0].DefaultView;
16 GridView1.DataBind();
17
18 con.Close();
19 }
是使用gridview自带的分页
@小格调: 你把GridView1_PageIndexChanging方法改一下试试:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gridview();
}
@天行健 自强不息: 嗯,真的可以了耶!!!哈哈!!!谢谢!!!!!!
@小格调: ^_^,记得结帖哦
@天行健 自强不息: 就是我还想问下,这个是什么原因呢?
@小格调: 这个说起来可就长了哦,你了解一个gridview分页原理就明白了
@天行健 自强不息: 我主要是想问下在后台用代码实现的属性设置就是说用“代码实现的”吗? 这些属性都和设计时直接设置属性是一样的啊?
@天行健 自强不息: 嗯 好的,我去了解下。谢谢!