Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   ViewState question (http://www.velocityreviews.com/forums/t711007-viewstate-question.html)

Ilyas 01-07-2010 03:16 PM

ViewState question
 
Hi all

I have the following in my aspx:

<asp:TextBox runat="server" ID="txt1" EnableViewState="false" /
>

+
<asp:TextBox runat="server" ID="txt2" />
=
<asp:Label runat="server" ID="lblResult" />

<br /><br />
<asp:Button runat="server" ID="btnAdd" OnClick="btnAdd_Click"
Text="Add" />

and in my code behind I have:
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnAdd_Click(object sender, EventArgs e)
{
lblResult.Text = (int.Parse(txt1.Text) + int.Parse
(txt2.Text)).ToString();
}

When I enter 1 in the first box and 2 in the second, then click the
add button I get the answer appearing but the first textbox still
remembers the number entered even though I said
EnableViewState="false"

I dont want the first textbox to remember its value across postback -
how can I achieve this?

Thanks

Gregory A. Beamer 01-07-2010 04:12 PM

Re: ViewState question
 
Ilyas <ilyas@igsoftwaresolutions.co.uk> wrote in news:5eacb098-018f-
4afa-aca0-9dafa4401112@e27g2000yqd.googlegroups.com:

> I dont want the first textbox to remember its value across postback -
> how can I achieve this?


Clear it after you use it.

This is NOT a viewstate issue, as the value has not been stored in
ViewState at that time. It is an issue with the default way a browser
based control works. Solution:


protected void ClearForm()
{
txt1.Text = string.Empty;
txt2.Text = string.Empty;
}

protected void btnAdd_Click(object sender, EventArgs e)
{
lblResult.Text = (int.Parse(txt1.Text) + int.Parse
(txt2.Text)).ToString();
ClearForm();
}

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************


All times are GMT. The time now is 01:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57