Question

We have a web control that we want to render into a div on the already loaded page. This needs to happen when the user clicks on a hyperlink in the menu.

The web control has some javascript files that are added to it dynamically in the C# code.

We also arent using aspx files, and want to load the web control onto a normal html page.

Please let me know what would be the best way to do this, or direct me to an article as I'm struggling to find anything useful on the web. any help is appreciated.

Was it helpful?

Solution

You can render your control dynamically in a string, send the string back and place it inside your div. Using ajax you call the code behind to render the control and return you the results.

    // load the control
    var oCConrol = Page.LoadControl("CustomControl.ascx");

    // here you need to run some initialization of your control
    //  because the page_load is not loading now.

    // a string writer to write on it
    using(TextWriter stringWriter = new StringWriter())
    {
      // a html writer
      using(HtmlTextWriter renderOnMe = new HtmlTextWriter(stringWriter))
      {
        // now render the control inside the htm writer
        oCConrol.RenderControl(renderOnMe);

        // here is your control rendered output.
        strBuild = stringWriter.ToString();
      }
    }

Alternative you can get the same results by just make an extra empty aspx page with only your control inside and call it using Ajax. The result will be again the control

The javascript how ever is dificult and can not go with the control. You can not send text back and then make it run as javascript inside the control, this must be done separated.

OTHER TIPS

I have done something similar by creating a separate, standalone .aspx page and Jquery ajax. Call to the .aspx page using Jquery ajax and return all the rendered HTML from that page via the ajax response.

As far as I know, there is not any way to render a web control outside the context of a .aspx page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top