vb.net 如何保护 asp:textbox 免受用户输入的影响?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22096984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to protect an asp:textbox from user input?
提问by DreamTeK
How to protect an asp:textbox from user input?
如何保护 asp:textbox 免受用户输入的影响?
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.
我希望保护上述文本框免受用户输入的影响,但仍允许使用 javascript 更新该值。
This works fine so what's the problem?
这工作正常所以有什么问题?
THE PROBLEM
问题
If a textbox is disabledor readonlythe updated values will not post to the server when submitted!
如果文本框是disabled或readonly更新的值在提交时不会发布到服务器!
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.
对用户可见。
防止用户输入。
使用 javascript 进行更新。
将更新的值发布到服务器。
ANSWER
回答
The conflict occurs because when you set enabled="false"on control it is does not pass the values to the server.
发生冲突是因为当您设置enabled="false"on control 时,它不会将值传递给服务器。
However setting disabledor readonlyclientside does not cause this issue.
但是设置disabled或readonly客户端不会导致此问题。
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 sidecontrols and setting them to runat server:
或者通过使用客户端控件并将它们设置为 runat 服务器:
<input type="text" id="txbx" readonly="readonly" runat="server" />
<input type="text" id="txbx" disabled="disabled" runat="server" />
回答by Atanu Roy
I once got out of this situation by setting the ReadOnlyproperty from code-behind. You can try this once.
我曾经通过ReadOnly从代码隐藏设置属性摆脱了这种情况。你可以试试这个。
txbx.Attributes.Add("ReadOnly", "ReadOnly");
Add this line of code in your Page_Loadevent.
在您的Page_Load事件中添加这行代码。
Please inform if it works for you.
请告知它是否适合您。
回答by Tobberoth
The easiest solution, in my opinion, is to have a hidden field and read that on postback instead, that way the protected textbox is just cosmetic, the actual logic is working with a hidden field.
在我看来,最简单的解决方案是拥有一个隐藏字段并在回发时读取它,这样受保护的文本框只是装饰性的,实际逻辑是使用隐藏字段。
That said, ReadOnly = trueshould still work on postback, it's only Enabled = falsewhich shouldn't postback the value.
也就是说,ReadOnly = true应该仍然可以进行回发,只是Enabled = false不应该回发值。
回答by Amir Sherafatian
you can use html inputelement instead, and set runatattribute to serverto refer that from server,like this:
您可以改用 htmlinput元素,并将runat属性设置为server从 引用该元素server,如下所示:
<input type="text" name="name" id="text2" readonly="readonly" runat="server" />
its value, will be sent to server after its changed on client, while it has set as readonly
它的值,将在客户端更改后发送到服务器,而它已设置为 readonly
回答by vittore
Not very well know for some reason, but ASP.NET has special property which controls if disabled controls being sent back to the server or not: HtmlForm.SubmitDisabledControls
由于某种原因不太清楚,但 ASP.NET 具有特殊属性,用于控制是否将禁用的控件发送回服务器:HtmlForm.SubmitDisabledControls
So what you have to do is set this property to truefor your Formcontrol
所以你要做的就是将此属性设置true为你的Form控件
<form id="form1" submitdisabledcontrols="true" runat="server">
<asp:TextBox ID="txbx" runat="Server" Text="1" ReadOnly="true" Enabled="false" />
</form>
回答by Gordon Mohrin
Beware: <form>does not send data for disabled inputs.
当心:<form>不发送禁用输入的数据。
My personal solution:
我的个人解决方案:
- Make original field invisible with JS
Write new field with the original value and make it disabled, just for show.
var status = document.getElementById("ElementID"); status.style.visibility='hidden'; var row = status.parentNode.parentNode; var newCol = row.insertCell(-1); newCol.innerHTML = "<div style='padding-left: 6px'><input readonly='readonly' type='text' size='50' value='" + status.value + "' class='inputReadOnly'/></div>";
- 使用 JS 使原始字段不可见
使用原始值编写新字段并将其禁用,仅供展示。
var status = document.getElementById("ElementID"); status.style.visibility='hidden'; var row = status.parentNode.parentNode; var newCol = row.insertCell(-1); newCol.innerHTML = "<div style='padding-left: 6px'><input readonly='readonly' type='text' size='50' value='" + status.value + "' class='inputReadOnly'/></div>";
回答by dipz
Be visible to the user.
对用户可见。
<asp:TextBox ID="txbx" runat="Server" />
Be protect from user input.
防止用户输入。
<asp:TextBox ID="txbx" runat="Server" Text="1" Enabled="false" />
<asp:TextBox ID="txbx" runat="Server" Text="1" ReadOnly="true" />
Be updated with javascript.
create a function and use like
使用 javascript 进行更新。
创建一个函数并使用像
<script type="text/javascript">
var a = ($("#<%=txbx.ClientID%>"));
a.value="assas";
</script>
Post updated value to server.
same as above
将更新的值发布到服务器。
同上

