Jon Kragh's Blog » Posts in 'Programming' category

Rendering an ASP.net UserControl to a String

I have been converting one of my side projects from ASP.Net WebForms to ASP.Net MVC.  In order to reuse some of my existing ASP.net UserControls from WebForms in ASP.Net MVC, I tweaked a rending method found here and here.

I improved on these methods by enabling the caller of the utility function to be the one to set properties on the UserControl in a strongly typed fashion. The is no reflection or any other special interface needed on the user control.  The key was adding a callback.

Here is what it looks like to call and render the control to a string by the caller.  In this example I am rendering my GoogleMap UserControl to a string.  The last argument I am passing is an anonymous method that is called by the utility function to initialize my control.

UIUtil.RenderUserControl<GoogleMap>("~/UserControls/GoogleMap.ascx",
    uc =>
    {
        uc.CollegeToShow = CollegeToShow;
        uc.Height = Height;
        uc.Width = Width;
        uc.Mode = Mode;
    });

The helper method is as follows:

public delegate void InitializeControlDelegate<T>(T ControlToUse);

public static string RenderUserControl<T>(string ControlPath, InitializeControlDelegate<T> InitControlCallback) where T : UserControl
{
    System.Web.UI.Page pageHolder = new Page();
    T ControlToRender = (T)pageHolder.LoadControl(ControlPath);
    pageHolder.Controls.Add(ControlToRender);
    InitControlCallback.Invoke(ControlToRender);
    StringWriter result = new StringWriter();
    System.Web.HttpContext.Current.Server.Execute(pageHolder, result, false);
    return result.ToString();
}

This method uses a similar technique as to the methods linked above, but it has a callback which calls back the anonymous method that I declared in the caller.  The callback method passes back a strongly typed UserControl as the argument that we can use to set properties on the initialized UserControl.

That is all you need to make it all work.  To make things a little nicer for myself I added “RenderToString” methods on my UserControls.  If you can’t update the UserControls themselves you can just stick code like this in a helper library.

public partial class GoogleMap : System.Web.UI.UserControl
{
    public static string RenderToString(Item ItemToShow, string Height, string Width, Constants.MapMode Mode)
    {
        return UIUtil.RenderUserControl<GoogleMap>("~/UserControls/GoogleMap.ascx",
            uc =>
            {
                uc.ItemToShow = ItemToShow;
                uc.Height = Height;
                uc.Width = Width;
                uc.Mode = Mode;
            });
    }
}

Now from within my Asp.Net MVC page (or any place else), I can render my ASP.net WebForms UserControl Google Map like so:

<%=GoogleMap.RenderToString(ItemToUse, "250px", "100%", Constants.MapMode.SingleItem)%> 

Cheers,

Jon

Twitter Syntax / Commands – Facebook should also support them

Something that amazes me on Twitter, is that regular humans (non-Twitter-Hashtag programmers) are fine with, even good at adding little markup tags in tweets.  So now that we have seen that the masses are cool with adding little tags and markup to freeform content, we should embrace it!

Since Facebook is the leading social network, they should also support a markup syntax in comments & status updates because it is useful for humans and computer programs.

It would be in Facebook’s best interest to improve the human user-Humansexperience and also in their interest to add semantics to status updates and comments so that they are more useful for Computer Programs. Facebook is trying to push what they call “Platform” which is a way to write computer programs that integrate with Facebook.

Here are a few of examples of how Twitter markup syntax is beneficial for humans and computer programs. Maybe with enough retweets of this article, it could actually happen :)

Twitter Replies and Mentions

Twitter supports the ability to reply and or mention another user and link to that user. From the Twitter getting started guide:

Any tweet beginning with @username is considered a reply.  We call tweets with @username elsewhere in the update mentions.

Example Twitter Mention

User benefits

  • Get mentioned and new people notice you
  • Notice people that other people are mentioning

Computer program benefits

  • Indicates some kind of relationship between users – Example algorithm: mentions between two users may mean that those two users are interested in each other

Twitter Hashtags

Twitter supports the ability to group tweets. From the Twitter getting started guide.

Because Twitter provided no easy way to group tweets or add extra data, the Twitter community came up with their own way: hashtags.  A hashtag is similar to other web tags- it helps add tweets to a category.  Hashtags have the ‘hash’ or ‘pound’ symbol (#) preceding the tag, like so: #traffic, #followfriday, #hashtag.  Hashtags can occur anywhere in the tweet: some people just add a # before a word they’re using, like so:

Hashtags are huge and they have so much potential that it is far too much to fathom, but I’ll take a stab:

User benefits

  • Easy way to tag / tie your post to a topic for others to find
  • Easy way to search on a topic
  • Real-time trends

Computer program benefits

  • Provides additional meaning to posts
  • Almost limitless possibilities

I could go on with more specific examples, but it boils down to this.  Markup in free-form content:

  • Makes new tools possible for users
  • Provides semantics for computer programs

At the end of the day, I really don’t Computercare if Facebook adds markup extensions.  However, I do think it would be good for the web, so that user-generated content has more meaning.  I would also push that if Facebook does implement some kind of markup, that they use the same popular markup that already exists on Twitter.  Standards are the backbone of the web.

Cheers,

Jon