AJAX GridView Paging and Sorting using C# in ASP.Net

by top54u.com 28 Apr, 2008
Spotlight.....

GridView Data Paging and Sorting using AJAX extensions also requires the same C# coding that we used to learn the GridView Data Binding, Paging and Sorting without AJAX extensions server controls.

 

To enable the GridView Paging and Sorting to update the GridView Data without refreshing the ASP.Net web page AJAX Extension controls are used. UpdatePanel is used to hold the GridView control that triggers the events for the controls placed inside it. By Default UpdatePanel triggers the asynchronous postbacks for its child controls.

 

You can use the following ASP.Net C# sample code to perform the asynchronous GridView Paging using AJAX UpdatePanel Control:

 

Click here to get the C# code for GridView Sorting. Use the GridView Sorting code by placing the GridView Control inside the UpdatePanel control by setting the AllowSorting = true similar to the following sample code for Paging where AllowPaging property of GridView control is set to true.

 

HTML Code for AJAX GridView paging

 

<form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
                <ProgressTemplate>
                    <asp:Label ID="lblProgress" runat="server" Text="Loading . . . . . "></asp:Label>
                </ProgressTemplate>
            </asp:UpdateProgress>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
                    </asp:GridView>
                    <asp:Label ID="lblErrorMsg" runat="server"></asp:Label>
                    <br />
                    <br />
                    <br />
                    <asp:Button ID="btnLoadData" runat="server" Text="Fill GridView" OnClick="btnLoadData_Click" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>

 

C# Code for AJAX GridView paging

protected void bindGridView()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
        SqlConnection sqlCon = new SqlConnection(connectionString);

        try
        {
            SqlCommand sqlCmd = new SqlCommand("select * from products", sqlCon);
            sqlCmd.CommandType = CommandType.Text;
            SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
            DataSet myDataSet = new DataSet();
            sqlAdp.Fill(myDataSet);

            // Delay the current Thread to display
            // the UpdateProgress status
            // Not required in real projects
            System.Threading.Thread.Sleep(4000);

            GridView1.DataSource = myDataSet;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message;
        }
    }

    protected void btnLoadData_Click(object sender, EventArgs e)
    {
        bindGridView();
    }

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

[/code]

 

You can use the Northwind Database to test the sample code. Place your connection string inside the web.config ConnectionString section.

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 9:15:02 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....