24

i have about 4 textboxes on my webpage...some are asp:textboxes while others are input type="text".

the input textbox is populated through a javascript popup calender control while asp.net textbox is populated by typing. The initial values of these textboxes are retrieved from a database.

When a user changes these values, they are not saved and the textboxes are cleared out after the submit button is clicked. Please help resolve this confusion. Thanks.

thanks for your reply but it is still not working.....

i have put this code in my page load event

if (Page.IsPostBack)
            {
                if (ViewState["stock"] != null)
                    TextBoxMaterial.Text = ViewState["stock"].ToString();

                if (ViewState["supplier"] != null)
                    TextBoxSupplier.Text = ViewState["supplier"].ToString();

                if(ViewState["matTime"] != null)
                    TextBoxMatTime.Text = ViewState["matTime"].ToString();

                if(ViewState["prodTime"] != null)
                    TextBoxProdTime.Text = ViewState["prodTime"].ToString();

                if (ViewState["shipTime"] != null)
                    TextBoxShipTime.Text = ViewState["shipTime"].ToString();

                if(ViewState["cmr"] != null)
                    cmrDue.Value = ViewState["cmr"].ToString();

                if(ViewState["kc"] != null)
                    kcDue.Value = ViewState["kc"].ToString();

}

and also put the below code in the onclick event for the button

ViewState["stock"] = TextBoxMaterial.Text;
            ViewState["supplier"] = TextBoxSupplier.Text;
            ViewState["matTime"] = TextBoxMatTime.Text;
            ViewState["prodTime"] = TextBoxProdTime.Text;
            ViewState["shipTime"] = TextBoxShipTime.Text;
            ViewState["cmr"] = cmrDue.Value.ToString();
            ViewState["kc"] = kcDue.Value.ToString();

            string prodLine = DDProdLine.SelectedValue;
            string stock1 = DDMaterial.SelectedValue;
            string stock2 = ViewState["stock"].ToString();
            string supplier = ViewState["supplier"].ToString();
            string billet = RBBillet.SelectedValue;
            string matTime1 = ViewState["matTime"].ToString();
            string matTime2 = DDMatTime.SelectedValue;
            string prodTime1 = ViewState["prodTime"].ToString();
            string prodTime2 = DDProdTime.SelectedValue;
            string shipTime1 = ViewState["shipTime"].ToString();
            string shipTime2 = DDShipTime.SelectedValue;

            CultureInfo cultureInfo = CultureInfo.CurrentCulture;
            string format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString();
            string cmr = ViewState["cmr"].ToString();
            string kc = ViewState["kc"].ToString();
            string x = cmr.Substring(3, 2);
            string y = cmr.Substring(0, 2);
            string z = cmr.Substring(6, 4);
            string x1 = kc.Substring(3, 2);
            string y1 = kc.Substring(0, 2);
            string z1 = kc.Substring(6, 4);
            string finalCmr = x + "/" + y + "/" + z;
            string finalKC = x1 + "/" + y1 + "/" + z1;

            DateTime dt = DateTime.ParseExact(finalCmr, format, cultureInfo);
            DateTime cr = DateTime.ParseExact(finalKC, format, cultureInfo);

            string custDate = dt.ToString("dd/mm/yyyy");
            string kcDate = cr.ToString("dd/mm/yyyy");
            string id = Request.QueryString["id"];
            bool success = true;

            TextBoxProdComment1.Text = stock2 + "," + supplier + matTime1 + "," + prodTime1 + "," + shipTime1 + "," + custDate
                + "," + kcDate;

            try
            {
                 success = CRTopButtons.SaveProdTable(id, prodLine, stock1, supplier, billet, matTime1, matTime2, prodTime1,
                    prodTime2, shipTime1, shipTime2, custDate, kcDate);
            }
            catch (Exception e)
            {
                TextBoxProdComment2.Text = e.Message;
                System.Diagnostics.Trace.Write(e.StackTrace);
            }

the textboxes still clear out and none of it is readonly..........

please help

8
  • 1
    How are you trying to save them? Can you reproduce the problem with minimal code and share that code here?
    – David
    May 3, 2011 at 12:32
  • 3
    are the standard input controls ran at server (ie runat="server")?
    – Curtis
    May 3, 2011 at 12:32
  • Is the viewstate enabled? Can you add the javascript code and the page load code?
    – Ivo
    May 3, 2011 at 12:33
  • What event is populating the textboxes from the database? is it during Page_Load?
    – Mark
    May 3, 2011 at 12:34
  • 4
    I'm guessing you are actually trying to set their values in page load and hence wiping out the values on postback - you may need to add a statement if(!IsPostBack) around the code where you are attempting to set the values from the database.
    – Dan
    May 3, 2011 at 12:36

7 Answers 7

33

I had a similar problem and The Solution to "Losing data changed by javascript during postback" is best described by this article ViewState and Readonly Property of Textbox

for example let's say we have this asp.net control:

<asp:TextBox ID="txtName" runat="server" EnableViewState= "false" ReadOnly="true" /> 

if you change the value of this control through javascript in the client side it will not be propagated via postback in the serverside...whatever you do with javascript unless you remove readonly="true". Now there is a solution to this problem as described in the article above.

Simply put this in the PageLoad event

if (!IsPostBack)
    txtName.Attributes.Add("readonly","readonly");

and you're done. Just don't forget to remove ReadOnly="true" or Enable="false" if your intent was to disable the control from editing just use the snippet above. Don't forget to remove Enable="false" if you put it on.

3
  • 2
    You sir just made my day. This was driving me nuts. +1 Sep 15, 2014 at 17:32
  • Hmm shouldn't it be txtName.Attribute.Add("readonly","true");?
    – Fandango68
    Jun 2, 2015 at 0:08
  • Rename Attribute to Attributes.
    – Jess
    Dec 18, 2015 at 16:27
9

Another thing I ran into... If you're using an ASP.NET TextBox Control (for example), and it's READONLY or DISABLED, the postback won't catch the changed value.

Per my issue, I was changing the value of the control thru javascript and even though the browser rendered the change, on the postback, the control still retained the original value.

Seems a common problem too... javascript kicks off a custom ASCX calendar control and result is injected by javascript, into the textbox. Users shouldn't be allowed to directly modify textbox value...

string strDate = Request.Form["id_of_input_element"].ToString(); 

I ultimately used the above to um, "reset" the control, after the postback, to it's changed value!

6
  1. The <input> textboxes won't save their state after postback. ASP.NET does not handle that for you.

  2. If you put code in your Page_Load event to set the values of the ASP.NET textboxes, the values that were posted back will not be saved, because Page_Load happens after the child control states are restored in the ASP.NET page lifecycle. The values are already restored by ASP.NET, but you are overwriting their restored values.

  3. The correct thing to do to fix #2 is to check Page.IsPostBack before loading your initial state, like this:

    if ( !Page.IsPostBack )
    {
        // set the textbox initial states from the database
    }
    

UPDATE:

There are two ways to solve problem #1. One thing you could do is to use the Request.Form[] collection to retrieve the posted back value manually, like this:

string strDate = Request.Form["id_of_input_element"].ToString();

The other thing you could do, and this is what I'd recommend if you can, is to change the <input> element to an ASP.NET textbox, and hook up any client-side Javascript events to that. Then ASP.NET will completely handle your postback.

2
  • Wow! thank you soooooo much, you helped me, I was getting very frustrated trying to figure out why all of my form values were blank upon postback! I used the Page.IsPostBack variable and that solved the problem. I did not realize initially that even on postback ASP.net will call the "Page_Load" method again! I had a method that would clear the form automatically on page load and this was also clearing the form on postback too! Thank you so much! Jun 8, 2011 at 20:06
  • 1
    My problem was similarly solved by checking this problem/condition. I forgot to set AutoPostBack on one of my DropDownList controls and so it was overwriting the TextBox value when I clicked the button because it was, at that time, trying to fill the text box thereby erasing the changes that the user made.
    – cjbarth
    Jun 23, 2012 at 0:19
3

i found this when looking for an answer to the same type of problem and now that i found my problem i thought it could help someone else putting it here.

in my case i had a tag <form> inside the <form> of my controls, so, if you didnt resolve your problem with above i sugest search for a <form> lost inside your <form>.

hope it helps for some cases.

0

If I get you're asking for right, I think you're trying to make those textboxes readonly. If so, I had this problem before and solved it by making the textboxes readonly using C# not ASP.NET, I just added lines like textboxName.Attributes.Add("readonly", "readonly"); in the Page_Load and it worked just fine. This solution I found here on Stackoverflow

0

instead of TextBoxPassword.Text=Password

use

TextBoxPassword.Attributes["value"]=Password
-1

It seems that your viewstate is disabled. Enable the viewstate in Page directive.

3
  • 1
    r u sure problem is view state? May 3, 2011 at 12:44
  • For me it seems to be a problem with the viewstate bec viewstate is acually responsible for getting the data back on postback. Similarly, if we have viewstate on, datatable need not to be rebind every time thus not pinging database on every postback. But keeping viewstate could be disasterous as well as entire data exists on the page thus page is retrieved late if lots of data is held by viewstate
    – FIre Panda
    May 3, 2011 at 12:53
  • please see my codes above and tell me what i'm doing wrong...thanks
    – bee
    May 3, 2011 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.