Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 13 Dec 2009
Rate this:
Please Sign up or sign in to vote.
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 | [Rose]

License

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

Share

About the Author

Abhishek Sur
Architect
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.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
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
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 13 Dec 2009
Article Copyright 2009 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid