开发者

CSS Formatting for ASP Controls

I have an开发者_Python百科 ASP:Label control on my page, and I would like to give it some CSS formatting. I know I could give it a CssClass name, however, it seems wrong because I want to give it a specific positioning therefore I'm looking for something more similar to the regular "id" attribute of html elements.

The ID of the label is the one used by the ASP, but in the actual html produced, I get a different ID.

Any suggestions? Thanks.


The next version of ASP.Net will make it easier to specify an exact clientID for the control. At the moment, you have several options to work around this issue:

Inline Styles

<asp:Label runat="server" ID="MyLabel" style="..." />

CssClass

Just use a css class, as you mentioned in your quesiton. Nothing stops you from making those unique if you have to.

Write a handler to serve your style sheet

When you write your style sheet, leave placeholder in the file for client IDs. Then use an http handler to substitute in the actual rendered IDs on each request. This isn't exactly simple because the style sheet request is separate from the html page request, but it is certainly possible and so worth mentioning.

Use a container

Since a label renders as a span tag, if that span is unique within a specific naming container you can select it that way:

<div id="MyContainer"><asp:Label ID="MyLable" runat="server" /></div>

And in your style sheet:

#MyContainer span { /*...*/ }

Use a container + a class

If the container is not specific enough, you can use the class just to narrow it down within that container:

<div id="MyContainer"><asp:Label ID="MyLable" runat="server" CssClass="MyClass"/></div>    

and in your style sheet:

#MyContainer span.MyClass { /*...*/ }


ASP.net essentially breaks the CSS ID selector. To get around this sometimes I will place this id in the CssClass attribute.


    <style type="text/css">
       input.first-name { /* style me */ }
    </style>

    <asp:TextBox runat="server" ID="firstName" CssClass="first-name" />

You can also add multiple class names in the CssClass attribute


    <style type="text/css">
       input.first-name { /* style me */ }
       input.text-input { /* because ie 6 won't do input[type=text] */ }
    </style>

    <asp:TextBox runat="server" ID="firstName" CssClass="first-name text-input" />

I try as much as possible to not use inline style or to use the additional styling attributes provided by the controls.


Your only options are to use CssClass or inline styles. As ASP.NET auto-generates the ID's of server side controls you should never try to second guess what these will be. It can be a major pain getting Webforms to work with elegant CSS layouts.

ASP.NET 4.0 will introduce the ClientID property that should make it easier to work with ID attributes in the future.


I think you can do something like:

<asp:Label ID+"lblID" style=" [whatever style you want goes in here "] runat="server />

Remember when the control gets rendered, it gets rendered as with the ctrl.etc....

Or if you are doing positioning, can't you wrap the label in a <div>


Yeah - a major headache with web forms - the id is made up of all the contentsections, panels etc that the label (or other control) sits within.

your best bet really is to add a CssClass to the control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜