如何使文本框在 asp.net(c#) 中不可编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11290356/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:16:02  来源:igfitidea点击:

How to make textbox as not editable in asp.net(c#)

c#asp.nethtml

提问by soundy

I've creating one ASP web application, in that application one form have to update date value in the textbox field i put calender button near that textbox. it can update the date in that textbox field but its editable . i want update date value through only using calender button , i used read-only property but return empty value so not works .

我创建了一个 ASP Web 应用程序,在该应用程序中,一个表单必须更新文本框字段中的日期值,我将日历按钮放在该文本框附近。它可以更新该文本框字段中的日期,但它是可编辑的。我想通过只使用日历按钮更新日期值,我使用只读属性但返回空值所以不起作用。

采纳答案by emre nevayeshirazi

Try client side html readonlyattribute instead of ASP.NET server side readonly.

尝试客户端 htmlreadonly属性而不是 ASP.NET 服务器端readonly

myTextBox.Attributes.Add("readonly", "readonly");

From MSDN,

从 MSDN,

The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.

发生回发时,将 ReadOnly 属性设置为 true 的 TextBox 控件的 Text 值发送到服务器,但服务器不处理只读文本框。这可以防止恶意用户更改只读的 Text 值。除非被服务器端代码修改,否则 Text 属性的值将保留在回发之间的视图状态中。

This is why textbox with server side readonlyattribute has null value in postback.

这就是为什么具有服务器端readonly属性的文本框在回发中具有空值的原因。

回答by patel.milanb

You might want to use this:

你可能想使用这个:

TextBox1.Enabled = false;

回答by patel.milanb

On page load event of your code behind file just write following code:

在您的代码隐藏文件的页面加载事件上,只需编写以下代码:

yourTextBoxName.Attributes.Add("readonly","readonly");

you can also do same by using property

你也可以通过使用属性来做同样的事情

Enabled="false"

on your aspx page but then you will not be able access its value in code behind page.

在您的 aspx 页面上,但是您将无法在代码隐藏页面中访问其值。

回答by Learning

You can use either TextBox1.Enabled = false;OR

您可以使用 TextBox1.Enabled = false;OR

TextBox1.Attributes.Add("readonly","readonly");

TextBox1.Attributes.Add("readonly","readonly");

Difference is that if you make enabled= falsethen you cant pass the value of the textbox. If you need to pass the value of the textboxthen you should use read-only property of textbox.

不同之处在于,如果你 makeenabled= false那么你不能传递textbox. 如果您需要传递 的值,textbox则应使用 的只读属性textbox

回答by Muhammad Omer Javed

You can make textBox non-editable in asp.net

您可以在 asp.net 中使 textBox 不可编辑

回答by user2478021

By using read only it will give problems on post back just set this java script property

通过使用只读它会在回发时出现问题,只需设置此 java 脚本属性

onkeydown="javascript:return false"

by using this u can have properties like readonly and have absolutely no problem will araise

通过使用它,您可以拥有 readonly 之类的属性,并且绝对不会出现问题