Wednesday, March 28, 2012

Why IsPostBack=false? How to repair that?

I'm using Ajax.Net. There is UpdatePanel on the page. It contains GridView. When I press "Edit", "Update" or "Cancel" button in the GridView then UpdatePanel reloads. Also executes code "if (!IsPostBack) { ... }" in Page_Load event on the server - but I don't need it!!! This code must run only once when page is loading for the first time.

protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) { ...// Here there are bindings for each control. // This code must run only once ...// On every action with GridView (which is placed in the UpdatePanel) // this code executes again and again. // Why? And how to prevent that? } }

1) Why IsPostBack=false (in Page_Load) on any action with GridView?

2) How to prevent that?

when you are clicking the button it's like you are reloading the all page but not the design... If you do not bind again the gridview loses data... Best solution is to cache the datatable that binds the gridview and binding the gridview to that... You can always verify the state of the grid to verify if you do some code...

As I understood, on every asynchronious postback (or mayme it's callback) the page is creating as it is not postback (isPostBack=false).

I think that it is better to use Session instead of Cache because every user has it's own data in the tabe.


cache in client... it's not postback... because the reference it's different... when you have difficulties test without the updatepanel to see if it's another reference of the page or not...

and i know what you're thinking the adress it's the same... it's ASP my friend... works that way.


What means "cache in client"? How? I can cache data only on server.


HttpContext.Current.Cache.Insert(itemString, Obj, HttpCacheability.Public, DateTime.Now.AddHours(expiration), TimeSpan.Zero)



Oh, it's caching on the server side, I thought you means caching data on the client side.

I change a bit my question. So, on each asynchronious postback executes code "if (!IsPostBack) { ... }" in Page_Load. But I want to prevent it. This code must executes only once when page is loading for the first time. How can I know if the page is loading for the first time or it is asynchronious postback? I want something like that: "if (!IsPostBack && !isAsynchroniousPostBack) { ... }"


did you tried with page.callback?


most likely no. what do you mean?


Put if not ispostback and not iscallback...


I wrote "!IsPostBack && !IsCallback && !IsAsync" - IsCallback and IsAsync always =false. IsPostBack in some cases =true and in some cases = false


tell me what are the cases that give true...

you don't have the need to put all of this...

if it doesn't control for you the postback you can put a viewstate that keeps the state of the grid.

just like this:

If not ispostback and viewstate("Should_Enter_Here")

end if

and in the event of the grid put this viewstate equals a true.


What you are looking for isIf (!ScriptManager.GetCurrent(Page).IsInAsyncPostBack).

http://www.asp.net/learn/ajax-videos/video-173.aspx


Thanks

No comments:

Post a Comment