<asp:GridView ID="GridView_AA" runat="server"DataKeyNames="ID,PERIOD_NAME,status" AllowPaging = "true"ondatabound="GridView_AA_DataBound" PageSize = "5" > <%--Pager--%> <PagerStyle forecolor="Blue" backcolor="LightBlue"/><PagerTemplate> <table> <tr> <td style="width:150px"> <asp:label id="MessageLabel" forecolor="white" text="Select a page:" runat="server"/> <asp:dropdownlist id="PageDropDownList" autopostback="true" onselectedindexchanged="PageDropDownList_SelectedIndexChanged" runat="server" Width="36px" /> </td> <td style="width:540px; text-align:center;"> <asp:label id="CurrentPageLabel" forecolor="white" runat="server"/> </td> <%--Go Command--%> <td style="width:200px; height:26px; text-align:right; float:right;"> Page : <asp:TextBox id="txtGo" runat="server" Width="30px" /> <asp:RangeValidator ID="rvtxtGo" runat="server" ControlToValidate= "txtGo" Type="Integer" MaximumValue="100" MinimumValue="1" ></asp:RangeValidator> <asp:Button id="btnGo" Text="Go" runat="server"OnClick="btnGo_Click" Width="30px" /> </td> <%--Go Command--%> </tr> </table> </PagerTemplate> <%--Pager--%> </asp:GridView>
protected void GridView_AA_DataBound(Object sender, EventArgs e) { GridViewRow pagerRow = GridView_AA.BottomPagerRow; DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel"); if (pageList != null) { for (int i = 0; i < GridView_AA.PageCount; i++) { int pageNumber = i + 1; ListItem item = new ListItem(pageNumber.ToString()); if (i == GridView_AA.PageIndex) { item.Selected = true; } pageList.Items.Add(item); } } if (pageLabel != null) { int currentPage = GridView_AA.PageIndex + 1; pageLabel.Text = "░ Page " + currentPage.ToString() + " of " + GridView_AA.PageCount.ToString() + " ░"; } } protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e) { GridViewRow pagerRow = GridView_AA.BottomPagerRow; DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); try { SqlConnection sqlConn = new SqlConnection(connStr); string sql = "SELECT * FROM ANNUAL_ASSESSMENT"; sqlConn.Open(); SqlDataAdapter da = new SqlDataAdapter(); da = new SqlDataAdapter(sql, sqlConn); DataSet ds = new DataSet(); da.Fill(ds, "PAGING_ANNUAL_ASSESSMENT"); foreach (DataRow row in ds.Tables[0].Rows) { row[2] = Config.StrYYYYMMDDtoDate(row[2].ToString()); row[3] = Config.StrYYYYMMDDtoDate(row[3].ToString()); } ds.AcceptChanges(); GridView_AA.DataSource = ds.Tables["PAGING_ANNUAL_ASSESSMENT"]; GridView_AA.PageIndex = Convert.ToInt32(pageList.SelectedValue) - 1; GridView_AA.DataBind(); sqlConn.Close(); ImageSettingGridview(); } catch (Exception ex) { Response.Write(ex.ToString()); } } protected void btnGo_Click(object sender, EventArgs e) { GridViewRow pagerRow = GridView_AA.BottomPagerRow; TextBox pageList = (TextBox)pagerRow.Cells[0].FindControl("txtGo"); try { SqlConnection sqlConn = new SqlConnection(connStr); string sql = "SELECT * FROM ANNUAL_ASSESSMENT"; sqlConn.Open(); SqlDataAdapter da = new SqlDataAdapter(); da = new SqlDataAdapter(sql, sqlConn); DataSet ds = new DataSet(); da.Fill(ds, "PAGING_ANNUAL_ASSESSMENT"); foreach (DataRow row in ds.Tables[0].Rows) { row[2] = Config.StrYYYYMMDDtoDate(row[2].ToString()); row[3] = Config.StrYYYYMMDDtoDate(row[3].ToString()); } ds.AcceptChanges(); GridView_AA.DataSource = ds.Tables["PAGING_ANNUAL_ASSESSMENT"]; GridView_AA.PageIndex = Convert.ToInt32(pageList.Text) - 1; GridView_AA.DataBind(); sqlConn.Close(); ImageSettingGridview(); } catch (Exception ex) { Response.Write(ex.ToString()); } }
I hope you can try by yourself according to the above example.
Reference: http://www.aspnettutorials.com/tutorials/controls/gvpagertemplate-csharp.aspx
No comments:
Post a Comment