Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET
Tip/Trick

Common issues to ASP.NET input controls for Enabled / Visible / ReadOnly Properties

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
13 Dec 2009CPOL1 min read 21.8K  
Hi Guys,It is common behavior of ASP.NET that if a control is set to Visible = "false", the response stream doesn't contain the definition of it. Therefore, if you want to make the control visible during runtime from Javascript always put display = none in styles attribute.Say you have a...
Hi Guys,

It is common behavior of ASP.NET that if a control is set to Visible = "false", the response stream doesn't contain the definition of it. Therefore, if you want to make the control visible during runtime from Javascript always put display = none in styles attribute.

Say you have a textbox which you want to make invisible but want to display when required from the browser.

<asp:textbox id="txt" runat="server" />

In codebehind instead of writing
txt.Visible=false;

You write:
txt.Style.Add(HtmlTextWriterStyle.Display, "none");

If you take the 2nd approach, the server will render the control and send to the client and finally will not be displayed because of display:none CSS style. You may again show the control using the following javascript :

var elem = document.getElementById("txt"); 
elem.style.display='block';


Note : It is better to use txt.ClientID instead of txt directly as identifier in Javascript.


Another issue of ASP.NET is with setting Enabled = false / Readonly=true.

In case of Enabled=false or Readonly = true, ASP.NET sends the input control to the browser, but any changes made in the control in client side will not reflect in the server.
That means, whenever the control is recreated in the web server during postback, it will not include the Form data posted for the control. Thus the initial value will be restored.

Therefore, to overcome the issue again, you should put
ctrl.Attributes.Add("readonly", "readonly");
ctrl.Attributes.Add("disabled", "disabled");


This will work the same way but the value changes in the client side will reflect to the server side controls.

So if you want to work with the controls in the client side using javascript, dont use restriction using properties from server side, rather use CSS Attributes to do this.

Hope you will like this. :rose:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions

 
-- There are no messages in this forum --