Render an ASP.NET Control but also Keep it Invisible

In order to hide a control, a lot of developers set the Visible property of the control to True. However what this does is that it does not Render the control, so you cannot set a value to the control.

If you want to Render the Control and also keep it invisible at the same time ‘programmatically’, use the CssStyleCollection. So for example, if you have three textboxes tbOne, tbTwo, tbThree and want to render the textbox tbTwo but keep it invisible, use the following code

tbTwo.Style.Add(HtmlTextWriterStyle.Display, "none");

What this essentially does is that it sets the display style of the textbox to none.

image

Note: You could have declared a Css class and then set the TextBox CssClass to it, but I wanted to demo an example that can be done programmatically at server side.



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

Anonymous said...

Hi, thank you, please correct the following: "In order to hide a control, a lot of developers set the Visible property of the control to -> False" instead of True.