Search code examples
asp.netstylescode-behindweb-deployment

setting Style property of a WebControl From Code Behind


recently I found in one of my older projects (asp.net 4.0) that I've been using this code

to set display Style-property To none

DDL_ChosenEmpl.Attributes.Add("style", "display:none");

lately I was using

DDL_ChosenEmpl.Style.Add("display", "none");

i would like to know

  1. in any case such as - property already exist whether it's with different value or not , will any of them should be avoided ?

  2. what are the main differences between both methods ?


Solution

  • Both does the same functionality as you have done. The thing is, in "Attributes" collection, you have a control's other attributes like "click", "dblclick", etc. You can use either way. I believe Microsoft has given the later part for user's convinience to make coding easy.