TextBox not keeping value after Postback

RecipeDan
RecipeDan used Ask the Experts™
on
Hello:

When I enter a value in the textbox the autopostback and OnTextChanged fires. The OnTextChanged does what it is supposed to do. However, after the postback the textbox does not keep its value.

<asp:TextBox ID="NameText" AutoPostBack="true" OnTextChanged="NameText_TextChanged" runat="server" />

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NameText.Text = Request.Form["NameText"];  
            }
         }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
Is ViewState turned off for the application?

Also, if you are only setting this value on initial load and not on postbacks then how could Request.Form["NameText"] be anything but null since your textbox does not appear to have an initial value. Of course, this would have nothing to do with the textbox retaining an entered value because it never runs on a postback.

Author

Commented:
I don't know what you mean about viewstate being turned off?

I have only used view state in this format before:

ViewState["NameText"] = NameText.Text;
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
if (!IsPostBack)
            {
                NameText.Text = Request.Form["NameText"];   
            }

Open in new window


shouldn't it be

if (IsPostBack)
            {
                NameText.Text = Request.Form["NameText"];   
            }

Open in new window


If you wanted it to work on a postback?

And also I don't understand why you need to do this. What are you trying to achieve?
Top Expert 2015

Commented:
OnTextBoxChanged only works if ViewState is turned on according to this:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.ontextchanged.aspx

It's on by default so you probably have it on.

@Chinmay_Patel is basically restating what I have already pointed out, i.e. that Request.Form["NameText"] in the (!IsPostBack) conditional will never have a value because it only runs when the page is opened initially. Hence, it cannot be causing this reported problem. We have to look elsewhere.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial