TryingtolearnNewStuff avatar image
0 Votes"
TryingtolearnNewStuff asked AlbertKallal-4360 commented

How do I get the value of a textbox in the server-side code?

I have the following snippet of code. When the user enters the page, there is a default value set in the text box. Now when the user changes the value and clicks a button, I am seeing the default value instead of the changed value.

Moving the code to !IsPostback block is not an option at this time. There are values coming in from the session object that need to be retained at all times.

 public partial class _Default : Page
         Models.InspectionModel model = new Models.InspectionModel();
         protected void Page_Load(object sender, EventArgs e)
             if (!IsPostBack)
             if (Request.QueryString.Count > 1)
                 //Create a session object.
                 model.Name = Request.QueryString["Make"].ToString();
                 model.Key = "Chrysler";
                 model.Id = -1;
                 Session["CurrentModel"] = model;
                 //This value needs to change
                 txtTest2.Text = "Hello My Name is LUKE";
         protected void Button1_Click(object sender, EventArgs e)
             model.Id = 2;
             model.Name = "Renegade";
             TextBox1.Text = $"Model Information {model.Id} name - {model.Name} key - {model.Key}";
         protected void btnCheck_Click(object sender, EventArgs e)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You did not provide the steps to reproduce this issue.

I created a test page with a default textbox value. I change the value, click a submit button, and the value I entered was submitted as expected. There must be bugs elsewhere in your code and/or design. Please run your code through the Visual Studio debugger and use the browser's dev tools to review what parameter values are being submitted.

0 Votes 0 ·

I am sorry for not providing enough. I cut out parts of the code that didn't make sense. The issue is that when I click the button, the page_load event gets called. As soon as it gets called, it realizes it is going to default the value.

0 Votes 0 ·

1 Answer

AlbertKallal-4360 avatar image
1 Vote"
AlbertKallal-4360 answered AlbertKallal-4360 commented

Gee, your page load sets the value of that text box to:

txtTest2.Text = "Hello My Name is LUKE";

that code (page load) will FIRE EVERY time. So for any post back - any button click, that code is running.

and it runs BEFORE your click button code stub.

You quite much HAVE to design your pages to survive post-backs, and that 99% of the time means that your control loads, grid loads, and controls setup code can ONLY run on the first page load, and that means yes, you quite much have to not only live with using !IsPostBack stub for this purpose, but actually means you can't really build and write a working web page unless you follow this design pattern.

From what I can "guess" here?
You jump to this page - passed some values as URL query parms. They need to be picked up, saved to view state, and ONLY done the first time on the first page load (!IsPostBack).

I can't think (or imagine) that any button click on the current page is going to use (or pass) URL parameters, so once again this suggests that you pick up those URL query values only on first page load.

From that point on, you assume that any button click etc. will trigger page load - but you don't care, since your intial code to set the text box, and grab the passed URL query values was safely picked in in that VERY important first page load.

if you move all of that URL query parm code, and the first setting of the text box into !IsPostBack stub, then your button code click stub should run fine, and not suffer any interference from that page load code.

You don't want to fight - it simply how it works, and thus your design pattern has to follow how this works.

Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is certainly helpful. You're right. Each time the button is clicked, the postback happens. I haven't done any webforms coding in a while and thanks for your suggestion. That is indeed helpful.

1 Vote 1 ·
AlbertKallal-4360 avatar image AlbertKallal-4360 TryingtolearnNewStuff ·

Appreciate the follow up. I really think a separate event called "first page load" should have been created. I cannot tell you the number of times I drop a simple dropdown list (combo box), let the user select and hit a button. And my value in the combo box is lost!!!

why? Because in page load I was loading up the combo box!!! so, I click on button, re-load combo box data source, and now my original combo box selection is gone!

so, we by near "instinct" have to put in in that if Not IsPostBack code stub in page load. That is the "real" first page load. And once we do that, everything becomes quite easy - but we can't fight this issue much.

Best of luck on your project.

0 Votes 0 ·