Solved

DataGrid looses columns on postback

Posted on 2004-08-06
8
351 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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