Behavior of "class" property on UserControl

Discussion in 'ASP .Net Building Controls' started by Alex Maghen, Jan 6, 2009.

  1. Alex Maghen

    Alex Maghen Guest

    Let's say I have a UserControl <Ax:MyCtl> and the only tag inside that ascx
    file looks like:

    <div id="MainDiv" class="InnerCssClass">
    SomeText Here

    Now, let's say I have a page that uses MyCtl...

    <Ax:MyCtl id="MyCtl1" class="OuterCssClass" />

    Here's what I've noticed: The "InnerCssClass" will be applied to the
    "MainDiv" div inside the control, but "OuterCssClass" which is set on the
    actual instance of the control on the page will have no effect.

    Do I have to actually implement a Property inside MyCtl called "class" and
    then apply it to the Div inside my control? Also on standard tags, I can use
    "class" *or* CssClass. For my control, do I have to implement a Property for
    both "class" and "CssClass"?

    Your help is much appreciated.

    Alex Maghen, Jan 6, 2009
    1. Advertisements

  2. Hi Alex,

    From your description I think you need the OuterCssClass take place of the
    InnerCssClass when it's set on the page. If so we can add a property for
    the UserControl. Here's the sample that demonstrates how to achieve the


    <%@ Control Language="C#" AutoEventWireup="true"
    Inherits="WebApplication2.WebUserControl1" %>
    <div id="MainDiv" runat="server" class="InnerCssClass">
    SomeText Here


    public partial class WebUserControl1 : System.Web.UI.UserControl
    protected void Page_PreRender(object sender, EventArgs e)
    if (this.CssClass != string.Empty)
    this.MainDiv.Attributes.Add("class", this.CssClass);

    public virtual string CssClass
    string str = (string)this.ViewState["CssClass"];
    if (str != null)
    return str;
    return string.Empty;
    this.ViewState["CssClass"] = value;



    <uc1:WebUserControl1 ID="WebUserControl11" runat="server"
    CssClass="OuterCssClass" />

    The key point is to add the class to div when the CssClass is not empty

    Please try it to see if it works. If you have further questions please feel
    free to ask.

    Allen Chen
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:

    Get notification to my posts through email? Please refer to

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Allen Chen [MSFT], Jan 6, 2009
    1. Advertisements

  3. Hi Alex,

    Have you solved this issue?

    Allen Chen
    Microsoft Online Community Support
    Allen Chen [MSFT], Jan 13, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.