Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

c#

asp.net

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 .

like image 327
soundy Avatar asked Jul 02 '12 08:07

soundy


2 Answers

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.

like image 133
emre nevayeshirazi Avatar answered Sep 21 '22 08:09

emre nevayeshirazi


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.

like image 22
Learning Avatar answered Sep 24 '22 08:09

Learning