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

No.of Views1180
Bookmarked0 times
Downloads 
Votes0
By  abhi2434   On  13 Apr 2010 10:04:18
Tag : ASP.NET , HTML and CSS
Common issues to ASP.NET input controls for Enabled / Visible / ReadOnly Properties
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


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. 

Thank you

 
Sign Up to vote for this article
 
About Author
 
abhi2434
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-22 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
By:Abhishek SurDate Of Posted:3/21/2011 2:28:01 PM
@Tomer
Well, javascript can do anything. So you must not consider javascript into account.
By:TomerDate Of Posted:3/20/2011 3:37:38 AM
Development Manager
For us this does not work. we use a dropdownlist and we tried to disable the user from updating by using ddlist.Attributes.Add("disabled","disabled") The dropdownlist was disabled on the client side but. When we change the value of the dropdoenlist using java script we see the new value but it is not posting back to the server. Why is that ?
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top