Learn how to a build a cloud-first strategyRegister Now


ASP.NET Postback advice?

Posted on 2007-08-06
Medium Priority
Last Modified: 2013-11-05
Hi Experts,

I am relatively new to ASP.NET although I've worked with win forms a lot.

In ASP.NET let's say you have a couple of private member variables and in the button click even you need to use these variables.

The postback is done before the handler gets called i.e. the variables are empty at the time you need them in the click event!!!

How can I manage this if the variables ARE NOT simple types like numbers and ints i.e. what if they are datatables, datasets, IList<> etc ...

For example I had the case where i had a datatable (and an IList<T> in another case) that I used as a datasource to a GridView. When the user clicked a row on the GridView I grabed the ID of that row and used it to find the correct row in the dataTable and do some processing ... but the datatable was NULL by the time I got to it!

So what's your adivce/best practises/url's on managing this?

Thanks a million
Question by:kateL
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 19643019
LVL 22

Expert Comment

ID: 19643030
you need to store that information as cahce and then load the cahce back into the class variables during the ON_LOAD event.

LVL 10

Accepted Solution

SystemExpert earned 400 total points
ID: 19643567

When you are using datagrid , and related stuff like featching Id , Edit row, Update row

you should bind the datagrid only first time when your page load or when you change any data and submit to database

so plz bind it while checking
If Not IsPostBack then

// Call your Bind function here
End if

but while binding the datagrid

say dg1 is your datagrid and ds is your datasource

If ds.table(0).rows.count >0 then
       dg1.DataSource = ds
end if

Thanks & Regards
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 19643660
Hi sabeesh, that article is just about simple datatypes. I am asking about object like datatables and datasets etc..

Hi TAD, do you mean use Page.Cache? That's an application wide variable, right? I actually would need it on a per user basis ... so those that mean I would need to use the session variable or get it fresh from the database each time?

Hi SystemExpert, so what if I have 3 dataTables and I need to dynamically create and populate an ASP Table with the data from these tables depening on user input. Then during various events onthe page I need some or all of these 3 datatables for processing. How should I keep these tables in memory or should I retrieve them from the database each time?

Thanks a million for the help everyone, I'm learning loads even thinking about my q's.
LVL 10

Expert Comment

ID: 19643711
I think you can bind only one datasource to a grid

Author Comment

ID: 19643822
hi SystemExpert, yea that's true. I'm not using them 3 table as a data source. They are not bound to anything on the page

So what if during various events of my page I needed data from these 3 non-databound datatables?
LVL 10

Expert Comment

ID: 19644254
If they required refresh during this events than
put those binding code outside the IsPostBack block else don't hit DB every round trip to server

Thanks & Regards

Expert Comment

ID: 19645690
you can store your datavalues in session objects

Session["name"] = datatable;

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 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