Data Bind and View State

E

Eran Amitai

I have the simplest web page (using code-behind):
<body>
<%# MyText %>
two buttons - button1 and button2
</body>

My code-behind web page is very simple too:
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button1;
protected string MyText;

private void Page_Load(object sender, System.EventArgs e)
{
//DataBind(); // Don't want to bind here!
}

// Web Designer code omitted
private void Button1_Click(object sender, System.EventArgs e)
{
MyText = "In button1 handler";
DataBind();
}
}

I don't have a handler for button 2 (it only submits the page), and I DON'T DataBind in the PageLoad function. I only DataBind in the button1 handler.

Here's my problem/question: when I click on button1, I see the text "In button1 handler" on the page, as expected. However, if I click button2, the text doesn't disappear, which was really surprising to me. How does the bound text persist there? I know it has something to do with view state, because the latter changes when button1 is initially pressed. That is also the only way the server can know what the string is. I just haven't seen any documentation linking between binding literal text and view state. How do I disable this (i.e. make sure the bound text isn't saved in view state)?

Thanks,
Eran
 
A

Andy Mortimer [MS]

Hi,
If you use <%= MyText %> you will find that it works as you expected

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/ht
ml/jscondisplayingfromaspnet.asp

Andy Mortimer [MS]
Please do not send email directly to this alias. This alias is for
newsgroup purposes only

This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top