简体   繁体   English

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

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

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. 我正在创建一个ASP Web应用程序,在该应用程序中,一个表单必须更新文本框字段中的日期值,我将日历按钮放在该文本框附近。 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 . 我想通过仅使用日历按钮更新日期值,我使用只读属性但返回空值,因此不起作用。

Try client side html readonly attribute instead of ASP.NET server side readonly . 尝试客户端html readonly属性而不是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. 将ReadOnly属性设置为true的TextBox控件的Text值在发生回发时发送到服务器,但服务器不对只读文本框进行处理。 This prevents a malicious user from changing a Text value that is read-only. 这可以防止恶意用户更改只读的Text值。 The value of the Text property is preserved in the view state between postbacks unless modified by server-side code. 除非由服务器端代码修改,否则Text属性的值将保留在回发之间的视图状态中。

This is why textbox with server side readonly attribute has null value in postback. 这就是为什么具有服务器端readonly属性的文本框在回发中具有空值。

You can use either TextBox1.Enabled = false; 您可以使用TextBox1.Enabled = false; OR 要么

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

Difference is that if you make enabled= false then you cant pass the value of the textbox . 区别在于,如果你使enabled= false那么你不能传递textbox的值。 If you need to pass the value of the textbox then you should use read-only property of textbox . 如果你需要传递的值textbox ,那么你应该使用的只读属性textbox

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页面上,但是您将无法在页面后面的代码中访问它的值。

你可能想用这个

   TextBox1.Enabled = false;

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

By using read only it will give problems on post back just set this java script property 通过使用只读它会在post back上给出这个java脚本属性的问题

onkeydown="javascript:return false" onkeydown =“javascript:return false”

by using this u can have properties like readonly and have absolutely no problem will araise 通过使用这个你可以拥有像readonly这样的属性,并且绝对没有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM