文本框失去回传值 [英] textboxes lose value on postback

查看:140
本文介绍了文本框失去回传值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的网页约4文本框...有些ASP:文本框,而其他输入型=文本。

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

输入文本框是通过JavaScript弹出日历控制人口而asp.net文本框,通过输入填充。这些文本框的初始值被从数据库中检索。

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.....

我已经把这个code在我的网页加载事件

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();

}

和也投入了onclick事件以下code的按钮

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..........

请帮忙

推荐答案

我也有类似的问题和解决方案为失去回发期间由JavaScript更改的数据
这篇文章是最好的形容
文本框的ViewState中和只读属性

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

例如让我们说我们有这个asp.net控制:

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

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

如果您通过JavaScript在客户端就不会在服务器端通过回传传播......无论你用JavaScript做更​​改此控件的值,除非你删除只读=真。现在有向与上述文章中描述此问题的解决方案。

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.

简单地说这在pageLoad的事件

Simply put this in the PageLoad event

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

就大功告成了。只是不要忘记删除只读=true或启用=假,如果你的目的是为了禁止从编辑控件中使用上面的代码片段。不要忘记删除启用=假,如果你把它。

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.

这篇关于文本框失去回传值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆