how to hold value in viewstate even after refresh of page

Posted by Shanky11 under ASP.NET on 9/21/2013 | Points: 10 | Views : 6056 | Status : [Member] | Replies : 12
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{

if (e.CommandName == "Edit")
{
HiddenField hidenfield = (HiddenField)e.Item.FindControl("hdnimageid");
id = hidenfield.Value;
int x = (int)ViewState["counter"];
x++;
ViewState["counter"] = x;
lblcounter.Text ="Items " +x.ToString();
//lblcounter.ForeColor = System.Drawing.Color.Red;
lblcounter.Attributes.Add("style", "font-size:12px; color:Red; font-weight:bold;");
checkuser();

}
}
page_load
ViewState["counter"] = counter;
class label variable
int counter = 0;
how can i increment this counter on every click of button(runat=server)




Responses

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
make sure that enableViewState true in gris view control

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 9/21/2013 [Member] Starter | Points: 25

Up
0
Down
<asp:Panel ID="pHeader" runat="server" CssClass="cpHeader"> <asp:Label ID="lblcounter" runat="server" />
<asp:Label ID="lblText" runat="server" /> <div style="float:right; background-color:Aqua">
<asp:Button ID="btnorder" runat="server" Text="Order"
onclick="btnorder_Click" /></div>
</asp:Panel>
my lable in a panel and that panel is in update panel

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
might be use static variable for counter while declaration

static int counter = 0

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
alternate
check the link
http://bytes.com/topic/asp-net/answers/876014-how-retain-viewstate-while-page-refresh-when-using-updatepanel

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 9/21/2013 [Member] Starter | Points: 25

Up
0
Down
by static it is coming but when i close and run the page again it again starts from where it left last time that is the problem in static variable

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
may that is the problem with cookies... maku sure to destroy static variable when app closed....

refer the following link for knowing about updatepanel with trigger events
http://msdn.microsoft.com/en-us/library/bb386454(v=vs.100).aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer http://stackoverflow.com/questions/1058863/can-i-avoid-having-an-updatepanel-that-kills-viewstate
http://bytes.com/topic/asp-net/answers/876014-how-retain-viewstate-while-page-refresh-when-using-updatepanel

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 9/21/2013 [Member] Starter | Points: 25

Up
0
Down
difference between global and static variable
i am using like this
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{

if (e.CommandName == "Edit")
{
HiddenField hidenfield = (HiddenField)e.Item.FindControl("hdnimageid");
id = hidenfield.Value;
int x = (int)ViewState["counter"];
ViewState["counter"] = x;
lblcounter.Text ="Items " +x.ToString();
//lblcounter.ForeColor = System.Drawing.Color.Red;
lblcounter.Attributes.Add("style", "font-size:12px; color:Red; font-weight:bold;");
checkuser();

}
}
class vaariable
static counter=0;
page load
viewstate["counter"]++;

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
a static variable is global to the application on one server.
a global variable is specific to page

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
ViewState is only used on a single page that handles postbacks. Once youredirect to the new page, the ViewState is lost.Session can persist the value across the application.So,even if you redirect to another page you don't lose the value.Static Variable will be shared across all instances of aspx page.Hence if you have two users of same aspx at the same time value is shared which is not good & better NOT to use static variables in a web scenario.

http://www.dotnetperls.com/global-variables-aspnet
http://www.codeproject.com/Questions/145316/Difference-between-static-variable-sessionstate-an

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 9/21/2013 [Member] Starter | Points: 25

Up
0
Down
if i am using static it agian starts from where it left even after debugging
why???????

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Shanky,
Load Postback Data stage: Though this stage has nothing to do with ViewState, it causes most of the misconception among developers. This stage only happens when the page has been posted back. ASP.NET controls which implement IPostBackEventHandler will update its value (state) from the appropriate postback data. The important things to note about this stage are as follows:
1) State (value) of controls are NOT retrieved from ViewState but from posted back form.
2) Page class will hand over the posted back data to only those controls which implement IPostBackEventHandler.
3) This stage follows the Load View State stage, in other words state of controls set during the Load View State stage will be overwritten in this stage.

Now with the above information, let us try to answer the question:

Why some controls retain values even after disabling the ViewState while others do not?
The answer is Controls which implements IPostBackEventHandler like Textbox, Checkbox, etc. will retain the state even after disabling the viewstate. The reason is during the Load Postback Data stage, these controls will get state information from Posted back form.

But controls like label which do not implement IPostBackEventHandler will not get any state information from posted back data and hence depend entirely on viewstate to maintain the state.

Reference:
http://www.codeproject.com/Articles/31701/ViewState-and-Postback

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response