Solved

DataGrid looses columns on postback

Posted on 2004-08-06
8
349 Views
Last Modified: 2008-02-01
Hello,

When I click a button I query a table and then bind the data columns as described below and I do something on a page that does a postback I then loose the columns.  How do I prevent that from hapening on postback?
Please show me the code.

Thanks



        Dim myBoundColumn1c As BoundColumn = New BoundColumn
        myBoundColumn1c.DataField = "col_3"
        myBoundColumn1c.HeaderText = "Offer Name"
        myBoundColumn1c.HeaderStyle.Font.Bold = True
        DataGrid2.Columns.Add(myBoundColumn1c)


        Dim objConn2 As New OleDbConnection("Provider=""xxx"";password=xxxUser ID=xxx;Data Source=xxx")
        Dim objCmd2 As New OleDbDataAdapter("Select distinct col_2, col_3, des, flag from data_cleaning where col_1='truetest_id' and model='" & x & "'", objConn2)
        Dim ds2 As DataSet = New DataSet
        objCmd2.Fill(ds2, "data_cleaning")
        DataGrid2.DataSource = ds2
        DataGrid2.DataBind()
        objConn2.Close()
0
Comment
Question by:dmontgom
  • 5
  • 3
8 Comments
 
LVL 8

Expert Comment

by:daffodils
ID: 11740686
ASP .NET creates a new instance of the Page class every time a request is made, so any "dynamically created variables" that you want to persist between requests (such as postbacks) have to to be added again on every request.

Make sure that the dynamically created Columns are added to the Controls collection on every postback.

0
 

Author Comment

by:dmontgom
ID: 11743160
How do I do that?  What do I put in my page load event.  What is the code?
0
 

Author Comment

by:dmontgom
ID: 11743197
I tried placing the following code in the page load event and it did not work.  Still lost the columns.  

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myBoundColumn1c As BoundColumn = New BoundColumn
        myBoundColumn1c.DataField = "col_3"
        myBoundColumn1c.HeaderText = "Offer Name"
        myBoundColumn1c.HeaderStyle.Font.Bold = True
        DataGrid2.Columns.Add(myBoundColumn1c)
end sub

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 8

Expert Comment

by:daffodils
ID: 11743280
Try putting it in Page_init
0
 

Author Comment

by:dmontgom
ID: 11743556
I got it to work by putting it in the pagload event with the below code.  However, when I solve one problem another keeps popping up.  I have a check box list in each row and upon post back the checkbox when checked now gets unchecked.  Any idea on preserving the chceck boxes?  Also, seems pretty ridiculous that I have to requry my database on every page load.  ANy way to make the data source accesable through out the session and just rebind it?  


Please be specific if you know the answer or solution.

Thanks



If IsPostBack Then

        Dim myBoundColumn1c As BoundColumn = New BoundColumn
        myBoundColumn1c.DataField = "col_3"
        myBoundColumn1c.HeaderText = "Offer Name"
        myBoundColumn1c.HeaderStyle.Font.Bold = True
        DataGrid2.Columns.Add(myBoundColumn1c)
     
        Dim objConn2 As New OleDbConnection("Provider=""xxx"";password=xxxUser ID=xxx;Data Source=xxx")
        Dim objCmd2 As New OleDbDataAdapter("Select distinct col_2, col_3, des, flag from data_cleaning where col_1='truetest_id' and model='" & x & "'", objConn2)
        Dim ds2 As DataSet = New DataSet
        objCmd2.Fill(ds2, "data_cleaning")
        DataGrid2.DataSource = ds2
        DataGrid2.DataBind()
        objConn2.Close()


end if
0
 

Author Comment

by:dmontgom
ID: 11744797
Well, I have come to the conclusion that I can not preserve what a user checks.  However, everything else is working great.

I have my grids in a panel with a radio control list to choose between grids.  I just reload everytime.  To save the checks, before the first grid goes, I would like to save what the user checked to a database table for reloading.  What event fires before I loose the check information in the grid and a new grid loads.

Thanks
0
 
LVL 8

Accepted Solution

by:
daffodils earned 125 total points
ID: 11745032
A little late checking in...

>> What event fires before I loose the check information in the grid and a new grid loads.

The final sign of life of a control is the Unload event that arrives just before the object is dismissed. In this event you should release / save any critical resource you might have.

private void DataGrid1_Unload(object sender, System.EventArgs e)
{
 // save to database here
}
0
 

Author Comment

by:dmontgom
ID: 11749203
I give up on the damn data grid control!!!!!!!!!!!!!!!!!!!!!!!

It is worthless when dealing with postback

The unload event did not work.  When I put code to save in that event, did not do squat.

Thanks anyway
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question