Solved

ASP.NET - UserControl - Design Time Rendering

Posted on 2009-04-08
7
835 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 47
How to show selected values from drop down and text box on the popup? 25 44
Record locking on classic ASP 3 64
Can Selenium do Load Testing? 2 61
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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