Solved

ASP.NET - UserControl - Design Time Rendering

Posted on 2009-04-08
7
832 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

18 Experts available now in Live!

Get 1:1 Help Now