Thursday, April 14, 2011

Html Element properties in c#

Hi all, How do it create/access my own properties for elements in C# that I will use in JS. and how do I access properties that are avaiable in Html but don't appear to be exposed in the c# set like the border property for tables I know I can do it with styles and classes, but it seems like a limp around as opposed to the most robust way to do it. Thanks in advance.

From stackoverflow
  • The Attributes property of the WebControl base class is what you're looking for. Example:

    MyControl.Attributes["myattr"] = "examplevalue";
    
    Praesagus : Thank you so much. That was it. Any suggestions for the table attribute: border. C# wants to set it to 0 by default and changing the attribute only adds another border=1.
    Ken Browning : There's a BorderWidth property on the Table class. Have you explored that property?
  • The most robust as well as the most correct way of doing it is though CssClass property and a class defined inside a .css file.

    One reason to this is that if you have a designer who only touches CSS, they can change styles without touching your C# source code. If you don't have a designated CSS person, layer separation is still beneficial - just imagine looking though source code to change border color.

    Separating CSS, source code and JS as much as you can is the advisable practice.

    Praesagus : Thanks for the larger picture considerations. I will keep that in mind as I choose which to put where.
    : I'd like to add that you can always use JS and DOM to modify CSS styles inline on the fly.

0 comments:

Post a Comment