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

Abhijit Shrikhande 377 Reputation points
2022-04-22T16:37:43.817+00:00

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)
        {
            Response.Write(txtTest2.Text);
        }
Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. Albert Kallal 5,586 Reputation points
    2022-04-23T01:01:42.463+00:00

    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 asp.net - it simply how it works, and thus your design pattern has to follow how this works.

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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.