Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET - UserControl - Design Time Rendering

Posted on 2009-04-08
7
Medium Priority
?
839 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 780 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

730 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