Data Grid Disappears after postback

Discussion in 'ASP .Net Datagrid Control' started by Hagai Amiel via .NET 247, Nov 17, 2004.

  1. Hello There
    I have created a fully programmatically datagrid
    each time I press any button, the datagrid disappears when it posts back
    how do I keep the grid on the screen without re-constructing it from scratch each time?
    I have tryed saving it on session variables without any success

    Following my code

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (! IsPostBack)
    {
    createColumns(); //creates the boundColumns of the grid
    BindGrid();
    Session["myDS"]=DS;
    Session["myDG"]=myUserDG;
    Session["myCom"]=comUsers;
    }
    else
    {
    DS=(DataSet)Session["myDS"];
    myUserDG=(DataGrid)Session["myDG"];
    comUsers=(SqlDataAdapter)Session["myCom"];
    comUsers.Fill(DS, "USERS");
    myUserDG.DataBind();
    }
    }

    private void BindGrid()
    {
    myUserDG.DataSource=CreateDataSet();
    myUserDG.DataMember="USERS";
    myUserDG.DataBind();
    }

    private DataSet CreateDataSet()
    {
    connString=ConfigurationSettings.AppSettings["conn"];
    con=new SqlConnection(connString);
    comUsers=new SqlDataAdapter("getDetails",con);
    comUsers.SelectCommand.CommandType=CommandType.StoredProcedure;
    try
    {
    DS = new DataSet();
    comUsers.Fill(DS, "USERS");
    return DS;
    }
    catch(SqlException ex)
    {
    Response.Write("Errore retrieving Data:" + ex.Message.ToString() );
    return null;
    }
    }
    -------------------------------
    From: Hagai Amiel

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>UpRcPybXz0ynLS8Zp5pJmA==</Id>
     
    Hagai Amiel via .NET 247, Nov 17, 2004
    #1
    1. Advertising

  2. Hagai Amiel via .NET 247

    Scott M. Guest

    It's not the DataGrid you need to worry about re-creating, all that it takes
    to do that is to make a new instance of your grid class. What you need to
    worry about re-creating is the datasource of that datagrid (which is
    probably a datagrid). I don't have C# code for you but you can add your
    DataSet to viewstate (viewstate.add("data", DataSet) on the first page call
    and then pull it out of viewstate on postbacks
    (CType(viewstate.item("data"),DataSet) and then re-bind your grid to this.


    "Hagai Amiel via .NET 247" <> wrote in message
    news:%...
    > Hello There
    > I have created a fully programmatically datagrid
    > each time I press any button, the datagrid disappears when it posts back
    > how do I keep the grid on the screen without re-constructing it from
    > scratch each time?
    > I have tryed saving it on session variables without any success
    >
    > Following my code
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (! IsPostBack)
    > {
    > createColumns(); //creates the boundColumns of the grid
    > BindGrid();
    > Session["myDS"]=DS;
    > Session["myDG"]=myUserDG;
    > Session["myCom"]=comUsers;
    > }
    > else
    > {
    > DS=(DataSet)Session["myDS"];
    > myUserDG=(DataGrid)Session["myDG"];
    > comUsers=(SqlDataAdapter)Session["myCom"];
    > comUsers.Fill(DS, "USERS");
    > myUserDG.DataBind();
    > }
    > }
    >
    > private void BindGrid()
    > {
    > myUserDG.DataSource=CreateDataSet();
    > myUserDG.DataMember="USERS";
    > myUserDG.DataBind();
    > }
    >
    > private DataSet CreateDataSet()
    > {
    > connString=ConfigurationSettings.AppSettings["conn"];
    > con=new SqlConnection(connString);
    > comUsers=new SqlDataAdapter("getDetails",con);
    > comUsers.SelectCommand.CommandType=CommandType.StoredProcedure;
    > try
    > {
    > DS = new DataSet();
    > comUsers.Fill(DS, "USERS");
    > return DS;
    > }
    > catch(SqlException ex)
    > {
    > Response.Write("Errore retrieving Data:" + ex.Message.ToString() );
    > return null;
    > }
    > }
    > -------------------------------
    > From: Hagai Amiel
    >
    > -----------------------
    > Posted by a user from .NET 247 (http://www.dotnet247.com/)
    >
    > <Id>UpRcPybXz0ynLS8Zp5pJmA==</Id>
     
    Scott M., Nov 18, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry

    User Control Disappears After PostBack - Why?

    Guadala Harry, Jul 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    10,290
    Guadala Harry
    Jul 2, 2004
  2. Hagai Amiel via .NET 247

    Data Grid Disappears after postback

    Hagai Amiel via .NET 247, Oct 17, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    174
    Scott Mitchell [MVP]
    Oct 18, 2004
  3. Hagai Amiel via .NET 247

    Data Grid Disappears after postback

    Hagai Amiel via .NET 247, Oct 31, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    188
    Scott M.
    Nov 14, 2004
  4. Hagai Amiel via .NET 247

    Data Grid Disappears after postback

    Hagai Amiel via .NET 247, Dec 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    145
    R. Thomas, aka Xtreme.Net
    Dec 24, 2004
  5. Hagai Amiel via .NET 247

    Data Grid Disappears after postback

    Hagai Amiel via .NET 247, Jan 23, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    344
    Mike Ryan
    Jan 24, 2005
Loading...

Share This Page