ViewContext in UserControls

Rate It (1)

Last post 04-03-2008 8:12 PM by tgmdbm. 1 replies.

Sort Posts:

  • ViewContext in UserControls

    04-03-2008, 1:45 PM
    • Member
      1 point Member
    • netstruc65
    • Member since 03-10-2008, 10:13 PM
    • Posts 8

    Its always null? When Html.RenderUserControl is called is the ViewContext not passed on?

  • Re: ViewContext in UserControls

    04-03-2008, 8:12 PM
    • Contributor
      4,358 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 9:08 AM
    • Posts 881
    • ASPInsiders
      TrustedFriends-MVPs

    Unfortunately, you're correct. Only the HtmlHelper and UrlHelper objects are initialised.

    You can get at the context by doing Html.ViewContext

    ---

    Here's the snippet from reflector

    private static string DoRendering(ViewUserControl instance, ViewContext context, object controlData, object propertySettings)
    {
      ViewPage sourcePage = new ViewPage();
      sourcePage.Controls.Add(instance);
      sourcePage.Url = new UrlHelper(context);
      sourcePage.Html = new HtmlHelper(context);
      SetUserControlProperties(instance, propertySettings);
      if (controlData == null)
      {
        instance.SetViewData(context.ViewData);
      }
      else
      {
        instance.SetViewData(controlData);
      }
      return HtmlExtensionUtility.RenderPage(sourcePage);
    }
    
    

     
     What *should* happen is sourcePage.ViewContext = context and they should do lazy initialization of the HtmlHelper, AjaxHelper, and UrlHelper objects.

     

Page 1 of 1 (2 items)