Gridview with updatepanel in C#


Gridview with updatepanel in C#
The updatepanel control in lets you to update the page without refreshing the whole page but only the specified content.

To use the updatepanel control with GridView in you need to use asyncpostbacktrigger triggers in your page.
To do that first add the gridview control in your .aspx page as below :

    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <asp:AsyncPostBackTrigger ControlID="datagridview" EventName="PageIndexChanging" />
                <asp:GridView runat="server" ID="datagridview" CssClass="mydatagrid" AllowPaging="True"
                    CellPadding="4" ForeColor="#333333" GridLines="None" BorderColor="#003300" BorderStyle="Solid"
                    BorderWidth="1px" Font-Size="11pt" Font-Names="Century" OnPageIndexChanging="datagridview_PageIndexChanging">
                    <RowStyle BackColor="#EFF3FB" BorderColor="Black" BorderWidth="1px" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#2461BF" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
                    <AlternatingRowStyle BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />

Here we are adding update panel control and specifying a trigger which will handle the event of gridview page index changing :
Don’t copy again

                <asp:AsyncPostBackTrigger ControlID="datagridview" EventName="PageIndexChanging" />

The script manager control is important as without it the page will produce an error at run-time

Open your Web.config file and define your connectionString :

    <add name="myconnectionString" connectionString="Data Source=HITESH\SQLEXPRESS;Initial Catalog=MyDatabase;User ID=hitesh;Password=789"/>

Now add the following code to your .cs page :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;
using System.IO;
using System.Text;

public partial class GridViewUpdatePanel : System.Web.UI.Page
    String myconnectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
    SqlConnection con;
    SqlDataAdapter adapter;
    DataSet ds;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    public void fillDataGrid()
        con = new SqlConnection(myconnectionString);
        cmd = new SqlCommand("select * from mytable", con);
        adapter = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adapter.Fill(ds, "mytable");
        datagridview.DataSource = ds.Tables[0];
    protected void datagridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        datagridview.PageIndex = e.NewPageIndex;

The code is complete and pretty simple to understand.

Now when you run the page the asyncpostbacktrigger postback trigger will be fired when the user clicks on next page and it will load without refreshing the whole page.


Comment below if you have any questions or just type Thanks to let me know, this post was useful to you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.