AsyncPostBackTrigger Gridview分页

时间:2013-09-13 01:37:33

标签: c# asp.net asynchronous triggers postback

我是ASP C#的新手,我想弄清楚如何在AsyncPostBackTrigger中控制gridview分页..这是我的代码:

  <div id="grid_layer">
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
      <ContentTemplate>
    <asp:GridView ID="GridView1" CssClass="result_grid" runat="server" CellPadding="3" ForeColor="Black" 
        GridLines="Vertical" BackColor="White" BorderColor="#999999" 
           BorderStyle="Solid" BorderWidth="1px" AllowPaging="True" PageSize="15" 
            >
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnSearch" />
    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanged" />
    <asp:AsyncPostBackTrigger ControlID="Gridview1" EventName="PageIndexChanging" />
    </Triggers>
     </asp:UpdatePanel>
    <br />
    <br />

    </div> 

我的aspx.cs代码:

 private void BindGridviewDataWebloan()
    {
        string ConnectionStringB =   ConfigurationManager.ConnectionStrings["conWebloan"].ConnectionString;
        using (SqlConnection connectionB = new SqlConnection(ConnectionStringB))
        {
            connectionB.Open();
            SqlCommand cmdWebloan = new SqlCommand("Select a.ldatetime as Date, b.action_name as Action, a.description as Description, a.username as Username from webloan.dbo.logbook a join webloan.dbo.action_def b on a.action_no=b.action_no where DATEDIFF(day,ldatetime,@date_exec) = 0", connectionB);
            cmdWebloan.Parameters.AddWithValue("@date_exec", txtDate.Text);

            SqlDataAdapter da = new SqlDataAdapter(cmdWebloan);

            DataSet ds = new DataSet();


            da.Fill(ds);
            GridView1.DataSource = ds;          
            GridView1.DataBind();
            connectionB.Close();
        }

    }

我没有使用SQLdatasource我正在使用数据集...谢谢..

1 个答案:

答案 0 :(得分:2)

GridView的PageIndexChanged / PageIndexChanging事件将以异步方式调用。虽然如果您希望在引发这些事件时刷新整个UpdatePanel控件,您可以为此类事件添加<asp:AsyncPostBackTrigger>Check MSDN

您只需要定义OnPageIndexChanging&amp; GridView的OnPageIndexChanged个事件。在Markup中将它们设置为:

<asp:GridView ID="GridView1"
     OnPageIndexChanging="GridView1_PageIndexChanging"
     OnPageIndexChanged="GridView1_PageIndexChanged"
...>
</asp:GridView>

OnPageIndexChanging事件处理程序中,您需要手动设置页面索引以及再次将数据绑定/填充到GridView。

protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        // must call your Data filling function, else gridView will be empty
        BindGridviewDataWebloan(); 
    }