Any issues with using LoadControl() in the web service?

[WebMethod]
public string GetMyUserControlHtml()
{
   UserControl temp = new UserControl();
   MyUserControl ctrl =
(MyUserControl)temp.LoadControl("~/MyUserControl.ascx");
   ctrl.MyProperty = "x";

   string retval;

   using ( StringWriter sw = new StringWriter() )
   {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            ctrl.RenderControl(sw);
            retval = sw.ToString();
   }

   return retval;
}

I'm also getting an empty result when trying to do it the way you have.
Seems to work Ok for WebControl (such as Label), but not UserControl.

[WebMethod]
public string Get()
{
   Label l = new Label();
   l.Text = "Hello world";

  string retval;
  using (StringWriter sw....)
  {
           HtmlTextWriter hw ...;
           l.RenderControl(hw);
           retval = sw.ToString();
  }

 return retval;
}

Ron

----- Original Message -----
From: "Ryan Heath" <[EMAIL PROTECTED]>
To: <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM>
Sent: Monday, September 04, 2006 12:46 PM
Subject: Re: [ADVANCED-DOTNET] UserControl & RenderControl method


Yup, that is basically what I am doing, but I dont get any content.

StringBuilder sb = new StringBuilder();
using ( StringWriter sw = new StringWriter( sb))
using ( HtmlTextWriter htw = new HtmlTextWriter( sw))
{
MyUserControl ctrl = new MyUserControl();
ctrl.MyProperty = value;
ctrl.RenderControl( htw);
}
return sb.ToString();

When I place the user control on a page it is rendering correctly.
I must be missing something obvious ...

// Ryan

On 9/4/06, Dominick Baier <[EMAIL PROTECTED]> wrote:
using (StringWriter sw = new StringWriter())

   {

       HtmlTextWriter htw = new HtmlTextWriter(sw);

       _grid.RenderControl(htw);

       htw.Flush();

       return sw.ToString();

   }





cheers,
dominick

-----------------------------
Dominick Baier, DevelopMentor
http://www.leastprivilege.com


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Ryan Heath
Sent: Montag, 4. September 2006 18:02
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] UserControl & RenderControl method

Hi,

How can I extract the html of an usercontrol?
The method renderControl is returning an empty content! :S

I want to use an usercontrol's content to be returned in a webservice.

I googled around but cannot find a solution...

// Ryan

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com


===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com


===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to