Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataGrid looses columns on postback

Posted on 2004-08-06
8
Medium Priority
?
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 375 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

688 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