Set viewstate variable at end of page lifecycle
Wha开发者_如何学JAVAt am I trying to do is to set a ViewState variable at the end of a page lifecycle: what I have, is completely loaded asp.net page - communicating with AJAX calls with client JavaScript... when the ajax call from the client JavaScript is performed, some C# methods from the server code are executed. During this execution, I want to set a ViewState variable and use it on the next postback, but apparently the ViewState variable is not stored - because the page is completely loaded - all logic is performed at the end of the page lifecycle.
I've tried explicitly calling these methods before and after setting the ViewState variable:
this.TrackViewState();
if (startAddr != "")
{
ViewState["startAddress"] = startAddr;
}
this.SaveViewState();
But with no success... on the next postback, the ViewState["startAddress"]
is null.
Any suggestions, if the above described functionality can be achieved?! Thanks!
You could override page's SaveViewState method to see if it called before your code. SaveViewState is the very last point where you can store objects in ViewState that are not part of the control-hierarchy.
protected override object SaveViewState()
{
if (startAddr != "")
{
ViewState["startAddress"] = startAddr;
}
return base.SaveViewState();
}
You shouldn't call directly SaveViewState method.
Check if EnableViewState is set to true in your page. And check if ViewState is really assigned.
As far as i know,
U don't need to store variable in the viewstate
,
if ur value is get from the web page,
as u r using Ajax to call back (not sure using CallBack
or PageMethod
),
u can get value without store it in viewstate
.
juz store the value in JS variable.
let me know, if any suggestion or confusion?
Hope it works!
精彩评论