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.






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 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.