Solved

ASP.NET - UserControl - Design Time Rendering

Posted on 2009-04-08
7
831 Views
Last Modified: 2012-05-06
I am designing a UserControl and I'd like to customize the design time appereance of the UserControl, ie, how it renders when dropped onto a page in Visual Studio.

How can the design time appeareance of a class inheirting from System.Web.UI.UserControl be modified?

I've tried overriding the Render and RenderControl methods, but this has not impacted how the control renders during design time, only how it renders when viewed within the browser.  Additionally, I've tried decorating the class using the System.ComponentModel.Designer attribute, but this did not work either.  

In the code attachments I've mocked up what I would expect the code to look like to further emphasize what I'm trying to do.

Any help would be greatly appreciated.

Thanks,

PJ
public partial class MyUserControl : System.Web.UI.UserControl

{

    protected override void RenderControl(HtmlTextWriter writer)

    {

         if (this.DesignMode)

            {

                writer.Write("Hello World!!! - Design Time");

            }

            else

            {

                writer.Write("Hello World!!! - Run Time");

            }

    }

}

Open in new window

0
Comment
Question by:ppittle
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24115082
I believe that what you need to look at is the GetDesignTimeHtml method, and how it is implemented.

ControlDesigner.GetDesignTimeHtml Method
http://msdn.microsoft.com/en-us/library/ew7620aw.aspx

0
 
LVL 8

Author Comment

by:ppittle
ID: 24116006
TheLearnedOne,

I have tried creating my own class that extends both ControlDesigner and UserControlDesigner and overrides the GetDesignTimeHtml method.  However, when I decarate by UserControl class with the Designer attribute that specifies the type of my custom designer class, there's no change to the Visual Studio designer surface:

public class MyDesigner : UserControlDesigner
{
   override string GetDesignTimeHtml()
{
//return custom string with Design time markup
}
}

[Designer(typeof(MyDesigner))]
public partial class MyUserControl : UserControl
{}

I've tried the above example using a WebControl instead of a UserControl and it works fine, the HTML specified in my MyDesigner class is shown on the VS Design Surface.  However, WebControls don't allow for declarative markup.  

Any additional thoughts?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24116323
Hmmm...missed the UserControl as a base class (d'oh).  I don't think that you can get design-time HTML with a UserControl.  That would only be for a WebControl, as you found out.

"However, WebControls don't allow for declarative markup."
That is true, but you should be able to work around that limitation.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Author Comment

by:ppittle
ID: 24117361
So you're saying I can't have my cake and eat it too?

Any idea why there would be such a limitation with User Controls?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 195 total points
ID: 24118981
A UserControl is a special case, and its usage dictates that design time HTML is not possible, but I don't know the specific reason for that choice.

What are you doing with declarative HTML markup that you can't do with a custom web control?
0
 
LVL 8

Author Comment

by:ppittle
ID: 24130938
I have a UserControl that contains a FormView and an ObjectDataSource, which is easier to set up declaratively.  I could convert it to a WebControl and add the FormView and ObjectDataSource programatically, but then its harder to maintain.

In my situation, the Control has a Property, FieldName, which is set by the Parent Control.  The FieldName Property is mapped to a Parameter in the ObjectDataSource.  Addtionally, the Control is used about 20 times on the Parent Control.   Ideally, I'd like the UserControl to display the value of the FieldName property when viewed in the Parent Control.  Instead, it displays a default FormView and ObjectDataSource.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24131091
All you would need to do in a custom web control is to generate the same HTML that you would declaratively in the user control, in order for it to work programmatically, so I fail to see why it would be harder to maintain, other than it would be in a separate project.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now