3

我对 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 我正在使用数据集...谢谢..

4

1 回答 1

2

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

您只需要为您的 GridView 定义OnPageIndexChanging&OnPageIndexChanged事件。在标记中将这些设置为:

<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(); 
    }
于 2013-09-13T04:05:03.767 回答