如何保护一个asp:文本框从用户输入? [英] How to protect an asp:textbox from user input?
问题描述
如何保护一个asp:文本框从用户输入
禁用
< ASP:文本框ID =txbx=服务器文本=1启用=FALSE/>
保护它
< ASP:文本框ID =txbx=服务器文本=1只读=真/>
我要保护的文本框如上来自用户的输入,但仍允许值使用JavaScript进行更新。
这工作得很好所以有什么问题呢?
问题
如果一个文本框为禁用
或只读
提交时更新的值将不会发布到服务器!
问
如何创建一个文本框的同时可以:
-
对用户可见的。
-
从用户输入的保护。
-
有JavaScript更新。
-
更新的Post价值的服务器。
答案
冲突的发生是因为当你设置启用=FALSE
上控制它的值不会传递给服务器。
但设置禁用
或只读
客户方不会导致此问题。
您可以通过使用客户方设置的属性:
在 $ C $后面ç
txbx.Attributes.Add(只读,只读)
txbx.Attributes.Add(禁用,已禁用)
或使用客户端的控制和设置他们的Runat服务器:
<输入类型=文本ID =txbx只读=只读=服务器/>
<输入类型=文本ID =txbx禁用=禁用=服务器/>
有一次,我从code-背后设置只读
属性得到了摆脱这种局面。您可以一次试试这个。
txbx.Attributes.Add(只读,只读);
添加此行code在你的的Page_Load
事件。
请告知它是否适合你。
How to protect an asp:textbox from user input?
DISABLE IT
<asp:TextBox ID="txbx" runat="Server" Text="1" Enabled="false" />
PROTECT IT
<asp:TextBox ID="txbx" runat="Server" Text="1" ReadOnly="true" />
I wish to protect the textbox as above from user input but still allow the value to be updated with javascript.
This works fine so what's the problem?
THE PROBLEM
If a textbox is disabled
or readonly
the updated values will not post to the server when submitted!
QUESTION
How to create a textbox that can simultaneously:
Be visible to the user.
Be protect from user input.
Be updated with javascript.
Post updated value to server.
ANSWER
The conflict occurs because when you set enabled="false"
on control it is does not pass the values to the server.
However setting disabled
or readonly
clientside does not cause this issue.
You can set the properties clientside by using:
In code behind:
txbx.Attributes.Add("ReadOnly", "ReadOnly")
txbx.Attributes.Add("disabled", "disabled")
or by using client side controls and setting them to runat server:
<input type="text" id="txbx" readonly="readonly" runat="server" />
<input type="text" id="txbx" disabled="disabled" runat="server" />
I once got out of this situation by setting the ReadOnly
property from code-behind. You can try this once.
txbx.Attributes.Add("ReadOnly", "ReadOnly");
Add this line of code in your Page_Load
event.
Please inform if it works for you.
这篇关于如何保护一个asp:文本框从用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!