Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

c# - textboxes lose value on postback

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...