Bongsky
Bongsky

Reputation: 503

AsyncPostBackTrigger Gridview Paging

I am really new to ASP C# and I want to figure out how to control gridview paging in AsyncPostBackTrigger.. here is my code:

  <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> 

my aspx.cs code:

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

    }

I am not using SQLdatasource i am using data set... Thanks..

Upvotes: 3

Views: 4076

Answers (1)

R.C
R.C

Reputation: 10565

The GridView's PageIndexChanged/ PageIndexChanging events will be called in asyncronous way. Although if you want the whole UpdatePanel control to refresh too when these events are raised , you can add <asp:AsyncPostBackTrigger> for such events . Check MSDN.

All you need is to define the OnPageIndexChanging & OnPageIndexChanged events for your GridView. Set these in Markup as:

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

And in your OnPageIndexChanging event handler, you need to set manually the page index as well as again bind/fill the data to your 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(); 
    }

Upvotes: 2

Related Questions