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

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

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

From 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.

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


You can use either 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. If you need to pass the value of the textbox then you should use read-only property of textbox.

Tags:

C#

Html

Asp.Net