当用js修改Label控件的值后,提交页面时,值并不被保留。
原因如下:
Label控件在客户端呈现为一个html的 span 标签,span标签并非表单元素(如input、select、textarea等),在提交表单时不会被提交到服务器端,所以用Label控件在客户端保存值而希望在服务器端得到其值是无法实现的。
<span id="Label1">Label</span>//Label对应的html
<input name="TextBox1" type="text" id="TextBox1" />//TextBox对应的Html
解决方案:
(1)可用HtmlInputHidden,即隐藏文本框来实现。
(2)用Input解决。实际上用TextBox也一样。
<input id="lblBeginTime" runat="server" type="text" tabindex="-1" readonly="readonly" style="background-color: Transparent; text-align: Left; border-style: none; width: 100px;" value='Test' />
方法步骤:
1、设置样式style="background-color: Transparent; text-align: Left; border-style: none; width: 100px;"
2、设置readonly="readonly"