Solved

DataGrid looses columns on postback

Posted on 2004-08-06
8
340 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
 
LVL 8

Expert Comment

by:daffodils
ID: 11743280
Try putting it in Page_init
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now