Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET - UserControl - Design Time Rendering

Posted on 2009-04-08
7
Medium Priority
?
840 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

877 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