Bruce
Bruce

Reputation: 2213

reading text of a text box

I am working on designing a user profile web page in asp.net using c#.

I first load the values of text boxes from database and put them in the text box:

txt_Name.Text = "somestring";

The user can then change the text in the text box to modify their profile.

However when I read txt_Name.Text it shows me the "original" value instead of what the user entered.

More clearly:

  1. First I set the value of a text box to something:
    txt_Name.Text = "somestring";
  2. Then the user changes the value of the text box to something else in the gui
  3. Then I read the value of text box:
    Response.Write(txt_Name.Text);

In 3 the value is the one from 1 instead of the one from 2

Upvotes: 2

Views: 3312

Answers (2)

Gaven
Gaven

Reputation: 371

Its all in the page life cycle have a look at this page

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Upvotes: 1

devdigital
devdigital

Reputation: 34369

It sounds like you aren't checking the Page.IsPostBack property (http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx) when you are setting the initial textbox value, so it is always being set no matter how the page is invoked.

private void Page_Load()
{
  if (!IsPostBack)
  {
     txt_Name.Text = "somestring";
  }
}

Upvotes: 5

Related Questions