所以,我想创建像“> 1 2 3 4 5 6<”这样的分页我决定在gridview中使用分页模板,这就是我所做的:
aspx文件:
<asp:GridView ID="gvDept" runat="server" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
AllowSorting="True" AllowPaging="True" AllowCustomPaging="True"
DataSourceID="DS" DataKeyNames="departementcode" PageSize="3" OnRowCommand="gvDept_RowCommand">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="cbSelectAll" onclick="SelectAll(this)"></asp:CheckBox>
<%-- <asp:CheckBox runat="server" ID="cbAll" OnCheckedChanged="cbAll_CheckedChanged" AutoPostBack="true"></asp:CheckBox>--%>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbSelectOne"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField text="Edit" DataNavigateUrlFields="departementcode"
DataNavigateUrlFormatString="ManageDepartement.aspx?flag=edit&departementcode={0}" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm ('Are you sure?');" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
<ItemStyle ForeColor="Red" />
</asp:TemplateField>
<asp:BoundField DataField="departementcode" HeaderText="Departement Code" ReadOnly="True" SortExpression="departementcode" />
<asp:BoundField DataField="departementname" HeaderText="Departement Name" SortExpression="departementname" />
<asp:BoundField DataField="createby" HeaderText="Create By" SortExpression="createby" />
<asp:BoundField DataField="createdate" HeaderText="Create Date" SortExpression="createdate" />
<asp:BoundField DataField="updateby" HeaderText="Update By" SortExpression="updateby" />
<asp:BoundField DataField="lastupdate" HeaderText="Last Update" SortExpression="lastupdate" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black"/>
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerSettings Mode="Numeric" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" CssClass="paging"/>
<PagerTemplate>
</PagerTemplate>
<RowStyle BorderColor="White"/>
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
<asp:LinqDataSource ID="DS" runat="server" ContextTypeName="Admin.AirAsiaDCDataContext"
EntityTypeName="" TableName="departements" EnableDelete="True" OnDeleted="DS_Deleted">
</asp:LinqDataSource>
然后我也把这段代码放在C#中:
protected void Page_Load(object sender, EventArgs e)
{
AirAsiaDCDataContext dc = new AirAsiaDCDataContext();
int dtcount = dc.departements.Count();
int pgcount = 0;
if (dtcount % 3 > 0)
{
pgcount = (dtcount / 3) + 1;
}
else
{
pgcount = pgcount / 3;
}
Button pagerBTN;
for (int i = 1; i <= pgcount; i++)
{
if(i == 1)
{
pagerBTN = new Button();
pagerBTN.ID = "btNext";
pagerBTN.Text = ">";
pagerBTN.CommandName = "Next";
gvDept.BottomPagerRow.Cells[0].Controls.Add(pagerBTN);
}
pagerBTN = new Button();
pagerBTN.ID = "bt" + i;
pagerBTN.Text = i.ToString();
pagerBTN.CommandName = i.ToString();
gvDept.BottomPagerRow.Cells[0].Controls.Add(pagerBTN);
if (i == pgcount)
{
pagerBTN = new Button();
pagerBTN.ID = "btLast";
pagerBTN.Text = "<";
pagerBTN.CommandName = "Last";
gvDept.BottomPagerRow.Cells[0].Controls.Add(pagerBTN);
}
}
}
protected void gvDept_RowCommand(object sender, GridViewCommandEventArgs e)
{
AirAsiaDCDataContext dc = new AirAsiaDCDataContext();
int datacount = dc.departements.Count();
int pagecount = 0;
int prevIDX = 0;
if (datacount % 3 > 0)
{
pagecount = (datacount / 3) + 1;
}
else
{
pagecount = datacount / 3;
}
if (e.CommandName == "Next")
{
if (gvDept.PageIndex < pagecount - 1)
{
prevIDX = gvDept.PageIndex;
gvDept.PageIndex = gvDept.PageIndex + 1;
if (prevIDX == pagecount - 2)
{
Button btn = (Button)gvDept.BottomPagerRow.Cells[0].FindControl("btNext");
btn.Visible = false;
}
}
}
else if (e.CommandName == "Prev")
{
if (gvDept.PageIndex > 0)
{
prevIDX = gvDept.PageIndex;
gvDept.PageIndex = gvDept.PageIndex - 1;
if (prevIDX == 1)
{
Button btn = (Button)gvDept.BottomPagerRow.Cells[0].FindControl("btPrev");
btn.Visible = false;
}
}
}
else
{
gvDept.PageIndex = Convert.ToInt32(e.CommandName) - 1;
}
}
我的代码工作,但问题是,如果我点击其中一个分页按钮,回发我之前生成的所有分页按钮完全消失后,我不明白我在这里做错了什么?任何更正?
答案 0 :(得分:0)
在网格视图中使用
OnPageIndexChanging="gvDept_PageIndexChanging"
在.cs文件中添加此代码,以便在每次页面索引更改时将数据绑定到网格视图。
protected void gvDept_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDept.PageIndex = e.NewPageIndex;
// write here function to bind the grid to the datasource
}
在Page_Load函数的开头添加它,以便每次回发后数据都会绑定到网格视图。
if (!IsPostBack)
{
//function to bind the grid to data source
}