Friday, 23 March 2012

paging,sorting in user control gride view

design the user control:

<asp:GridView ID="gv" runat="server" AllowSorting="True" onsorting="gv_Sorting"
        AllowPaging="True" onpageindexchanging="gv_PageIndexChanging" PageSize="2"></asp:GridView>
 c# code of User control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class WebUserControl : System.Web.UI.UserControl
{
    SqlConnection cn;
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    string s;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["x"] = 1;
        }
    }
  
  
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (Convert.ToInt32(ViewState["x"]) == 1)
        {
            ViewState["x"] = 2;
            DataTable dt = (DataTable)ViewState["dt"];
            s = "";
            s= e.SortExpression.ToString()+" asc";
            dt.DefaultView.Sort = s;
            gv.DataSource = dt;
            gv.DataBind();
        }
        else
        {
            ViewState["x"] = 1;
            DataTable dt = (DataTable)ViewState["dt"];
            s = "";
            s = e.SortExpression.ToString() + " desc";
            dt.DefaultView.Sort = s;
            gv.DataSource = dt;
            gv.DataBind();
        }
    }
    public void view()
    {
        ViewState["query"] = query;
        ViewState["con"] = con;
        cn = new SqlConnection(ConfigurationManager.ConnectionStrings["" + ViewState["con"].ToString()  + ""].ToString());
        cn.Open();
        da = new SqlDataAdapter(ViewState["query"].ToString(), cn);
        da.Fill(dt);
        gv.DataSource = dt;
        ViewState["dt"] = dt;
        gv.DataBind();
    }
    public string query
    {
        get;
        set;
    }
    public string con
    {
        get;
        set;
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)ViewState["dt"];
        gv.DataSource = dt;
        gv.DataBind();
    }
}
Add a new page in ur project and design the page :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc" TagName="UserControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
  <asp:UpdatePanel ID="up" runat="server">
  <ContentTemplate>
  <uc:UserControl ID="uc" runat="server" />
  </ContentTemplate>
  </asp:UpdatePanel>
   

    </div>
    </form>
</body>
</html>

the page looks like :



C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uc.query = "select * from emp2";
        uc.con = "DatabaseConnectionString";
        uc.view();
    }
}
 



No comments:

Post a Comment