extracting a UserControl's HTML ?

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default extracting a UserControl's HTML ?

    hello,

    i have a usercontrol in my app that draws a DropDownList and some text.
    elsewhere, i have a need to extract the control's HTML as if it had
    been rendered in-page.

    typically, when i need to do this for ASP.NET controls, i do the
    following:

    //some control
    HyperLink link = new HyperLink();
    link.Text = "sometext";
    link.NavigateUrl = "http://www.yahoo.com";

    //get its HTML
    StringBuilder sb = new StringBuilder();
    System.IO.StringWriter sw = new System.IO.StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    link.RenderControl(htw);

    string html = sb.ToString();


    however, this technique doesnt seem to be working for my user control:

    //my control
    Controls.MyControl myControl = new Controls.MyControl();

    //get its HTML
    StringBuilder sb = new StringBuilder();
    System.IO.StringWriter sw = new System.IO.StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    myControl.RenderControl(htw);

    string html = sb.ToString();

    ....the var html is always empty.

    am i missing something, or doesnt this work for usercontrols?


    thanks!
    matt

    matt@mailinator.com Guest

  2. Similar Questions and Discussions

    1. Object reference error in UserControl's Load event
      I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this...
    2. UserControl's Page_Load firing automatically
      Hi I created a default webform and a default webusercontrol with vs2003. I then added the usercontrol to the form and browsed the page. As...
    3. extracting dlls from cab
      Internet Component Download: I've created cab file which must extract a list of dlls to the user's machine. Since there is problem in extracting...
    4. Extracting HTML source from Javascript
      I am trying to use the CFHTTP command to pull HTML source into a variable. The problem I am running into is that the HTML page is using a javascript...
    5. Extracting web data
      Can anyone point me to java packages which aid in parsing web pages? TIA matt
  3. #2

    Default Re: extracting a UserControl's HTML ?

    ok, i believe ive found one mistake:

    //my control
    Controls.MyControl myControl = new Controls.MyControl();

    i think may need to be:

    //my control
    Controls.MyControl myControl =
    (Controls.MyControl)LoadControl("~/myControl.ascx");


    but, that gives me a new error:

    Control 'ddlReports' of type 'DropDownList' must be placed inside a
    form tag with runat=server.

    ....ddlReports is a dropdownlist w/i my usercontrol. the control is
    evidently barfing because it is being asked to render itself directly,
    w/o being inside of a server-side <form> tag.


    so, what to do? putting a server-side <form> tag inside the control
    seems problematic, because i wont be able to place it on any other
    webform, since that would produce *two* <form> tags.

    additionally, i do not want a <form> tag when i extract this
    usercontrol's HTML. (tho i could probably parse it out).

    how does one attain this seemingly simple goal of extracting a
    usercontrol's rendered HTML...?


    thanks!
    matt

    matt@mailinator.com Guest

  4. #3

    Default Re: extracting a UserControl's HTML ?

    I've had the same issue before. My solution was simple because i didnt WANT
    the dropdownlist so i had to visible=false those form controls. I agree, you
    prolly dont want to add a <form> inside your UC.

    I wonder if you could do a

    HtmlForm form = new Htmlform();
    myControl.Controls.InsertAt(0,form);

    Only insert that HtmlForm inside your export function. See if that will do
    the trick.

    HTH,
    T

    <matt@mailinator.com> wrote in message
    news:1152824930.183537.270560@m73g2000cwd.googlegr oups.com...
    > ok, i believe ive found one mistake:
    >
    > //my control
    > Controls.MyControl myControl = new Controls.MyControl();
    >
    > i think may need to be:
    >
    > //my control
    > Controls.MyControl myControl =
    > (Controls.MyControl)LoadControl("~/myControl.ascx");
    >
    >
    > but, that gives me a new error:
    >
    > Control 'ddlReports' of type 'DropDownList' must be placed inside a
    > form tag with runat=server.
    >
    > ...ddlReports is a dropdownlist w/i my usercontrol. the control is
    > evidently barfing because it is being asked to render itself directly,
    > w/o being inside of a server-side <form> tag.
    >
    >
    > so, what to do? putting a server-side <form> tag inside the control
    > seems problematic, because i wont be able to place it on any other
    > webform, since that would produce *two* <form> tags.
    >
    > additionally, i do not want a <form> tag when i extract this
    > usercontrol's HTML. (tho i could probably parse it out).
    >
    > how does one attain this seemingly simple goal of extracting a
    > usercontrol's rendered HTML...?
    >
    >
    > thanks!
    > matt
    >

    Anthony Merante Guest

  5. #4

    Default Re: extracting a UserControl's HTML ?


    Anthony Merante wrote:
    > I wonder if you could do a
    >
    > HtmlForm form = new Htmlform();
    > myControl.Controls.InsertAt(0,form);
    this does not work, either. same thing, even when i find the control
    from w/i that form object.

    MS - is there no way to do this?? how does one get the HTML of a
    *usercontrol* that contains server-side controls??


    matt

    matt@mailinator.com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139