Data Grid Disappears after postback

  • Thread starter Hagai Amiel via .NET 247
  • Start date
H

Hagai Amiel via .NET 247

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;
}
}
 
M

Mike Ryan

Hello,

Try placing createColumns() outside the IsPostBack condition. You can do
the databind on the initial page load, but you'll need to re-create your
dynamic grid every time.

- Mike
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top