3

私は本当に ASP C# に慣れていないので、AsyncPostBackTrigger でグリッドビュー ページングを制御する方法を理解したいと思っています。ここに私のコードがあります。

  <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();
        }

    }

私はデータセットを使用しています...ありがとう..

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 に答える