Click here to Skip to main content
13,737,154 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi, in asp.net im using c# and I have a ASP TextBox, I set the text of it to the current username in the code, then there is an update button to save it. When I check the text of it after changing it from like "X1" to "X2" it still returns "X1" instead of "X2".

What I have tried:

Code that run's when page loads:
UsernameTextBox.Text = DataValue.Action("Users", "Username", "SessionID", value);

What's ran when the button is clicked:
System.Windows.Forms.MessageBox.Show(UsernameTextBox.Text);


TextBox:
<asp:TextBox runat="server" ID="UsernameTextBox" class="form-control" />


Button:
<asp:Button runat="server" id="Save" OnClick="Save_Click" class="btn btn-primary" Text="Save Changes" />
Posted 14-Jun-18 14:29pm
Updated 15-Jun-18 4:45am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Not entirely clear, but the most likely cause is that your "Code that run's when page loads" is not inside an if (!IsPostBack) { ... } block.

When you click the button, the Page_Load event fires. If you don't check the IsPostBack property, you end up running your code again, overwriting whatever value the user has entered.

Test the property before loading the value:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        UsernameTextBox.Text = DataValue.Action("Users", "Username", "SessionID", value);
    }
}


NB: System.Windows.Forms.MessageBox.Show will NOT work in an ASP.NET application. The code is running on the server, and that is where the messagebox will appear.

It might seem to work when you're debugging the code in Visual Studio; but that's only because, in that specific case, the server and client are the same computer.

As soon as you deploy your code to a real server, one of two things will happen. The best case scenario is that the code throws an exception telling you that you can't display a messagebox in a non-interactive process. The other option is that the messagebox appears on the server, where nobody will ever see it, and your code hangs waiting for someone to log in to the server and click "OK" on the hundreds of waiting messages.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 15 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100