Passing "Style" along to constituent controls

A

Alex Maghen

I have a control I've created that has only one constituent control: A
DropDownList.

I want to expose a "Style" property for my control which, when "set", will
simply set that Style string for the constituent DropDownList.

First, the DropDownList.Style property is read-only. But beyond that, it's
some type of collection, rather than just a string.

So How do I pass this style down to the constituent control? This is
especially important becuase I need my whole control to support Absolute
positioning, and I don't know how to do that without the Style property.

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your post!

DropDownList.Style is of type CssStyleCollection, while
DropDownList.ControlStyle is of type Style. Based on my understanding,
you're trying to expose the CssStyleCollection type of style, correct? If
there's anything I have misunderstood, please feel free to post here.

DropDownList.Style is read-only, but you can set its Value property to
apply CSS properties. Since the Style property is already part of
WebControl (or CompositeControl), you can use the declarative Style
property as normal, for example:

<m:MyDropDownList Style="border-style:none; background:black" ID="ddl1"
runat="server"/>

All you have to do is override RenderContents in your control:

protected override void RenderContents(HtmlTextWriter writer)
{
_ddl.Style.Value = this.Style.Value;
_ddl.RenderControl(writer);
}

Hope this helps. If anything is unclear, please feel free to post here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex Maghen

I'll try this out, but a few questions:

1. Why do I have to override RenderContents? Why can't I just create a
property called "Style" in my control and have that property's SET do the
"_ddl.Style.Value = this.Style.Value;"? Then later, when the control renders
*itself*, it'll do what needs to be done?

2. In any case, do I still need to create a "Style" property for my control,
or is that automatically provided because of inherrtance from the Control
class?

Alex
 
A

Alex Maghen

ALSO, when I create a "Style" propert for my control manually that looks like:

public string Style
{
get
{
return (DDL.Style.Value);
}
set
{
DDL.Style.Value = value;
}
}

This works when I run the page. But it does NOT work when I want to use
Style and absolute positioning in the Design view in the Visual Studio
WYSIWYG form designer. Is that because I didn't do the "override
RenderContents" thing?

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your update!

The Style property is inherited from WebControl and you don't need to
create it again, what you need to is simply delegate the Style property's
value to your constituent control.

And you don't have to override RenderContents, but we need to ensure that
the Style property gets delegated to the constituent control before
actually rendering it. Actually the best method to override is PreRender:

protected override void OnPreRender(EventArgs e)
{
_ddl.Style.Value = this.Style.Value;
}

It's all about the life cycle and event sequences, please refer to
following MSDN documentation:

#ASP.NET Page Life Cycle Overview
http://msdn2.microsoft.com/en-us/library/ms178472.aspx

Regarding your last post about creating a "Style" property of string type,
although it works in runtime, it's not recommended because:

1) It hides the same named property Style of "CssStyleCollection" data type.
2) It doesn't work in designer because: for a WebControl in designer, it
bypasses the normal life cycle described in above MSDN documentation. For a
CompositeControl, it uses CompositeControlDesigner as its design time
designer. Its GetDesignTimeHtml implementation is:

public override string GetDesignTimeHtml()
{
this.CreateChildControls();
return base.GetDesignTimeHtml();
}

The GetDesignTimeHtml() internally calls the Render method. Because you
only applied DDL.Style.Value in the your Style property, the
GetDesignTimeHtml() has no chance to apply it again.

For more information about design-time support, please refer to following
documentation:

#ASP.NET Control Designers Overview
http://msdn2.microsoft.com/en-US/library/wxh45wzs.aspx

Hope this helps. If anything is unclear, please feel free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex Maghen

Walter -

Thanks for your response. Unfortunately, I'm still having all kinds of
trouble. What I have right now is a control that

1. Inherits from "UserControl". Should it inherit from "WebControl" instead?
What's the difference?

2. Has no "Style" property. You said in your message that that Propert
whould be inherritted, but unfortunately, I'm getting the error "does not
contain a definition for 'Style'"

3. Does the OnPreRender thing you suggested:
protected override void OnPreRender(EventArgs e)
{
_ddl.Style.Value = this.Style.Value;
}

Unfortunately, with all this, I'm still unable to use positioning, etc. in
the Form Designer in Visual Studio. Any ideas? Is my problem that I'm
inherritting from UserControl instead of WebControl???

Alex
 
A

Alex Maghen

I created a UserControl that's incredibly simple to test this out. Here's all
the code for the control. The control only has one constituent control,
"DDL", a DropDownList.

The code-behind for this control looks like:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AxStyleTestCtl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
DDL.Items.Add(new ListItem("Item", "Item"));
}

protected override void OnPreRender(EventArgs e)
{
//***Next line gives me: "'AxStyleTestCtl' does not contain a
//definition for 'Style'"
DDL.Style.Value = this.Style.Value;
}

}

Note the error marked with *** above.
 
C

CaffieneRush

UserControl does not inherit from WebControl so you will not have to
worry about shadowing the Style property in WebControl but it also
means you'll need to expose it yourself.

'Expose ddl's style as this user control's style
Public Property Style As CssStyleCollection
Get
Return DDL.Style
End Get
Set(ByVal value As CssStyleCollection)
DDL.Style = value
End Set
End Property

Regards,
Andy

Inheritance Hierarchy
System.Object
System.Web.UI.Control
System.Web.UI.TemplateControl
System.Web.UI.UserControl
 
A

Alex Maghen

I'm really confused about the difference between a WebControl and a
UserControl When I go to create a control in VS, the only thing I see is "Web
User Control" - this becomes a class derived from System.Web.UI.UserControl.
Is this what I *should* use?

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your update.

First, I must appologize for incorrectly assuming you're building a custom
server control, while in fact you're building a user control.

I will try to describe these two control authoring modes briefly.

A user control is a server control that you author in the same WYSIWYG and
declarative style as an ASP.NET page and save as a text file with an .ascx
extension. You do not have to precompile a user control. When a user
control is used in an .aspx page, the page parser dynamically generates a
class from the .ascx file and compiles it into an assembly. This
combination of page-style authoring and no-compile deployment makes it easy
to develop a user control.

In the contrary, a server control which is authored in code and deployed in
the form of compiled class libraries will be referred to as custom control.

These two models are geared for different scenarios. In general, the user
control model is well suited for authoring in-house, application-specific
controls, while the custom control model is better suited for authoring
generic and redistributable controls.

We will compare custom control and user control authoring model in
following aspects:

1) Deployment

Deployment is the most important factor to consider when choosing between
the two control-authoring models.

The custom control model is designed for authoring redistributable
components in the form of an assembly (compiled class library) that can be
used by a number of applications.

The user control model is designed for single-application scenarios.

2) Authoring

Custom controls are authored by writing a managed class that derives
directly or indirectly from System.Web.UI.Control in a .NET programming
language. There is no designer support for authoring custom controls.

At run time, the dynamically compiled user control class also indirectly
derives from System.Web.UI.Control. However, user controls are authored
declaratively in the form of .ascx files, which is very similar to the way
ASP.NET pages are designed and developed.

3) Content and Layout

Custom controls are very well suited to dynamic content presented in a
programmatically generated layout.

Because the layout of a user control is declared at authoring time within
the .ascx file, user controls are a much better choice when you want
relatively static content with a fixed layout.

4) Design-Time Behavior

Visual Studio .NET supports a wide range of RAD design-time capabilities
for a custom control. Design-time behavior can range from simple
customization of design-time display to enhanced property editing in the
property grid to template editing and data binding. Custom controls can
also be assigned a customized icon and can be placed on the toolbox of the
design-time environment. In addition, custom controls can be associated
with custom designers that provide the same RAD experience on a design
surface as the standard set of ASP.NET controls.

User controls have minimal design-time support in the current version of
Visual Studio .NET.

For more information about the difference between these two models, you can
refer to MSDN documentation or a book published by MS PRESS: <<Developing
Microsoft ASP.NET Server Controls and Components>>, by Nikhil Kothari,
Vandana Datje.

I hope this explanation will help you decide what control-authoring model
to use in your project. If anything is unclear, please feel free to post
here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top