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
    </div>

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

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

    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
     
    Alex Maghen, Jan 6, 2009
    #1
    1. Advertising

  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
    requirement:

    Ascx:

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

    Ascx.cs:

    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
    {
    get
    {
    string str = (string)this.ViewState["CssClass"];
    if (str != null)
    {
    return str;
    }
    return string.Empty;
    }
    set
    {
    this.ViewState["CssClass"] = value;
    }
    }


    }

    Aspx:

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

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

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

    Regards,
    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
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    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
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Allen Chen [MSFT], Jan 6, 2009
    #2
    1. Advertising

  3. Hi Alex,

    Have you solved this issue?

    Regards,
    Allen Chen
    Microsoft Online Community Support
     
    Allen Chen [MSFT], Jan 13, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huobazi
    Replies:
    1
    Views:
    700
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    424
    Rajesh Tiwari
    Aug 12, 2003
  3. nail
    Replies:
    0
    Views:
    1,550
  4. Fabiano
    Replies:
    2
    Views:
    609
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  5. Replies:
    0
    Views:
    484
Loading...

Share This Page