在Gridview外部使用Pager Panel进行Gridview Paging ASP.NET

时间:2014-07-25 04:16:16

标签: c# asp.net .net gridview paging

这是我第一次使用ASP.NET开发网站。

我想在带有分页功能的GridView中显示数据库中的数据,我可以使用OnPageIndexChanging="GridView1_PageIndexChanging"来实现它,但我想使用自己的寻呼机,所以问题是

“如何将我的寻呼机(图中的右下角)链接到gridview而不是由ASP.NET生成的寻呼机”

图片:image

这是我在aspx中的代码

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered table-condensed table-striped table-primary table-vertical-center"
        PageSize="3" AllowPaging="True"
        OnPageIndexChanging="GridView1_PageIndexChanging">
         <Columns>
              <asp:BoundField DataField="UNIT_ID" HeaderText="รหัส" SortExpression="unitid">
              <HeaderStyle CssClass="center" />
              <ItemStyle Width="10%" CssClass="center" />
              </asp:BoundField>
         </Columns>                                                                    
      </asp:GridView>
     </ContentTemplate>
</asp:UpdatePanel>

cs中的代码

public partial class _Default : Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                bindGridView();  
        }

        protected void bindGridView() {
            string sqltxt = "select * from drug_units"; //where UNIT_ID =:unitid";
            CommandData comm = new CommandData();
            comm.SetCommandText(sqltxt);
            //comm.AddInputParameter("unitid", "5");
            List<DrugsUnit> dy = new List<DrugsUnit>();
            comm.ExecuteNonQuery();
            dy = comm.ExecuteToList<DrugsUnit>();
            GridView1.DataSource = dy;
            /*BoundField boundField = new BoundField();
            boundField.DataField = "UNIT_ID";
            boundField.HeaderText = "ID";
            boundField.SortExpression = "ID";
            boundField.HeaderStyle.CssClass = "center";
            boundField.ItemStyle.CssClass = "center";
            GridView1.Columns.Add(boundField);*/
            GridView1.DataBind();
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bindGridView();
        }
 }

1 个答案:

答案 0 :(得分:0)

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Pager)
    {
        upGridPager.Update();
        e.Row.SetRenderMethodDelegate(new RenderMethod((w, r) =>
        {
            e.Row.SetRenderMethodDelegate(null);
            using (var ms = new StringWriter())
            using (var writer = new HtmlTextWriter(ms))
            {
                e.Row.RenderControl(writer);
                GridPager.InnerHtml = "<table>" + ms.ToString() + "</table>";
            }
        }));
}

aspx看起来像这样(在你的网格之外)

<asp:UpdatePanel ID="upGridPager" runat="server" UpdateMode="Conditional">
   <ContentTemplate>
     <div runat="server" id="GridPager" />
   </ContentTemplate>
</asp:UpdatePanel>