ASP.NET Postback advice?

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
kateLAsked:
Who is Participating?
 
SystemExpertCommented:
Hi,

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
than

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

Thanks & Regards
0
 
sabeeshCommented:
0
 
_TAD_Commented:
you need to store that information as cahce and then load the cahce back into the class variables during the ON_LOAD event.

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
kateLAuthor Commented:
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.
0
 
SystemExpertCommented:
I think you can bind only one datasource to a grid
Thanks
0
 
kateLAuthor Commented:
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?
0
 
SystemExpertCommented:
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
0
 
sabeeshCommented:
you can store your datavalues in session objects

Session["name"] = datatable;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.