Behavior of "class" property on UserControl

A

Alex Maghen

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
 
A

Allen Chen [MSFT]

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:
(e-mail address removed).

==================================================
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.
 
A

Allen Chen [MSFT]

Hi Alex,

Have you solved this issue?

Regards,
Allen Chen
Microsoft Online Community Support
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top