Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Postback questions

Status
Not open for further replies.

sheykc

Programmer
Sep 28, 2001
79
US
I'm struggling with the postback on .NET forms. I'm obviously not understanding something.

In the declarations of my page I have this:
Code:
Dim dsPricing As DataSet
In the form load function I have this code:

Code:
If Not Page.IsPostBack Then
  If dsPricing Is Nothing Then
    If blPricingFlag = False Then
       dsPricing = oPricing.GetPricingGridByBTN(strBillToNum, dtExpirationDate)
    Else
        dsPricing = oPricing.GetPricingGridByContractID(iContractID)
    End If
  End If
  BindPricing()
End If

I have a BindPricing() function that populates my datagrid called grdPricing.

Code:
    Public Sub BindPricing()
        'Bind the grid to the sorted data view.
        Dim dv As New DataView(dsPricing.Tables(0))
        dv.Sort = _sSort
        grdPricing.DataSource = dv
        grdPricing.DataBind()
    End Sub

The problem is, I do a lot of calculations on the grid. If a user changes the value inside of a textbox, it needs to update other things on the grid. The textbox calls CalcTotals(). But when I try to access the dataset insde of CalcTotals, that I made public originally (dsPricing) it says it's "Nothing". So I rebuild the dataset, which in turns wipes out everything the user just changed. I know I don't want to rebuild this dataset on the fly.

Why isn't my dataset staying public? Why does it disappear? This is a huge page of code, so I have not posted everything. Maybe there is something else I'm doing that's wiping out the dataset.
 
The problem is that your data isnt persistent, so when a button is pressed, the system reloads the page, but is doing a post back.

Look into putting your variable in a session variable, or into a control, so it is persisted.
 
Can you store a dataset as a session variable?
 
Yes you can:
To store the dataset:
Code:
   Session("myDataSet") = dsPricing

To retrive the dataset:
Code:
   dsPricing = Session("myDataSet")
 
Just keep in mind anything you store in session will remain there for the entire session, unless you unload it explicitly. That can add up rather quickly if you're storing one or more datasets per form and then multiplied per user.

I recently came from an applications programming background and I struggled with the whole postback thing as well. I immediately wanted to start using the session state to make it so I could work on things just like in applications programming, storing tables in session. The problem was I couldn't find anywhere to unload them, so they just sat there consuming resources. In mid to high traffic environments this solution would bring a server to it's knees.

As mentioned, controls for the most part will maintain their state in viewstate, including the data in them, and this is stored through postbacks so the controls and data you placed in them will still be there for you to do what you wish with them in the code behind when the page posts back.

Where it really starts getting fun is working with dynamically created controls. While their state remains in viewstate you have to make sure you manually recreate the controls (and in order :( ) when the page initializes on postback, otherwise your postbacks aren't going to work because the controls won't exist so the event you were trying to capture will never fire (such as OnClick).

Isn't web programming fun?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top