Dynamically loading usercontrol in asp.net page resulting exception :Control ‘Button1’ of type ‘Button’ must be placed inside a form tag with runat=server

Posted: January 23, 2011 in ASP.Net
Tags: , , ,

There are many times when you want to retrieve and manipulate the rendered HTML in code before its displayed to the user.Dynamic Loading of ASP.NET User Controls is required before you render the html & it can be done using the LoadControl Method.

Control control = this.LoadControl("~/WebUserControl.ascx") as Control;

If you try to use the LoadControl method of page,the drawback of this is that it has no control over where on the page the user control will actually appear.Say you have a button in user control & you are trying to load the user control with LoadControl method of page,then it will throw exception as below

Control ‘Button1’ of type ‘Button’ must be placed inside a form tag with runat=server.

Hence I suggest,use the LoadControl method of UserControl as below

UserControl userControl = new UserControl();
Control control = userControl.LoadControl("~/WebUserControl.ascx") as Control;

Below method allows to pass control and it will return the rendered HTML as a string

public string ControlRender(Control control)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
control.RenderControl(htw);
return sb.ToString();
}

Advertisements
Comments
  1. nazo says:

    Hi there, thanks for this, I am getting really hard time to make this to work, your above code works greate but I have this issue that I am trying to solve it more then 2 month, my problem is I am able to render all the content of the ascx control to string but I am losing all of my ascx codebehind events, the only event that works is the ascx Page_Load, but if I have a click for any asp button dose not work, I am losing all click events of the ascx codebehind, hope that is clear, and any ASAP help is appreciated. thanks.

  2. Anupama says:

    For added control to work in postback data processing, the control should be added in the Init event rather than in the Load event.Are you calling LoadControl in the Page_Load event now?

  3. nazo says:

    Hi and thanks for you reply, yes I was doing on Page_Load but I jjust changed it to Init when I read your comment and it is still doing the same issue I get the rendered control but button click wont work. here is my code:

    protected void Page_Init(object sender, EventArgs e)
    {
    UserControl userControl = new UserControl();
    Control control = userControl.LoadControl(“~/login.ascx”) as Control;

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    control.RenderControl(htw);
    Literal1.Text = sb.ToString();

    }

  4. Anupama says:

    If you do not have a specific reason to load this way,why dont you simply load it as below?Not sure,if it satisfies your scenario but events fire for controls in usercontrol.

    protected void Page_Init(object sender, EventArgs e)
    {
    UserControl userControl = new UserControl();
    Control control = userControl.LoadControl(“~/WebUserControl.ascx”) as Control;
    this.PlaceHolder1.Controls.Add(control);
    }

  5. nazo says:

    I do have a reason to do it this way, I have snippet strings in my pages that is coming from database so I want to replace like string.Replace(“[LOGIN]”, ascxcontrol); if I do Controls.Add(control) i can’t specify the locaion of the ascx where to add. how am I going to replace snippet [LOGIN] wiht my ascx control by doing Controls.Add()? hope that is clear! thanks.

  6. Anupama says:

    I just created a sample to reproduce your issue.Looking at the Literal Text what i could understand is,even if you have used a server side button,the server side behaviour is lost when converted to html.Take a look at this which i took from Literal Text:

    function Validate() {
    debugger;
    }

    input type="submit" name="Button1" value="Button" id="Button1"
    input id="Button2" type="button" value="button" onclick="Validate();"

    On my usercontrol,Button1 is aspx button & Button2 is html.Eventhough I have an onclick on aspx control,after rendering it’s lost.Hence I would suggest you to put a html button on usercontrol & call your respective method from onclick.You will not have any issues getting this fired.

  7. nazo says:

    hemm, but that’s not going to call server side function its going to call JS function! how is it going to work?

  8. Anupama says:

    With html,i am not sure how we can can make the button work same like aspx button..you should somehow handle it from javascript i beleive.Why dont you seek advice from some forums like StackOverFlow,Codeproject etc?

  9. nazo says:

    Hi, I know hwo to make it work with javascript but I dont want to do that because the whole CMS will always relay on JS thats not a good way of doing it, I should make both options available so the developer can choose which way to go. thanks yes I already posted on StackOverFlow and many other places waiting! but thanks for your help.

  10. Anupama says:

    ya,I understand.Good Luck!Meanwhile if I get a solution,will post here.

  11. dipen says:

    anupama great help

  12. HI . Anupama great help regarding load dynamically usercontrol in asp.net application thanks

  13. onednd says:

    Nazo did you manage to persist the events? im fighting the same issue trying to load a control from web service. Please let me know if you have any updates. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s