Solved

DataGrid looses columns on postback

Posted on 2004-08-06
8
352 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
Technology Partners: 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

Independent Software Vendors: 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!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
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…

729 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