How can I find span from code behind and add user control to it?

13,769
//This is some <span id="span1" runat="server"> </span>

//In the code behind...

var span1 = this.FindControl("span1") as HtmlGenericControl;
var controlPath = "path of your user control";
var someControl = LoadControl(controlPath);
span1.Controls.Add(someControl);
Share:
13,769
Sid M
Author by

Sid M

Updated on June 28, 2022

Comments

  • Sid M
    Sid M almost 2 years

    I've a div

    <div id="PageContent" runat="server"></div>
    

    Now I'm adding HTML to it from code behind(c#) like this

    PageContent.InnerHtml =mytext;
    

    where mytext is the content I want to add in PageContent div, mytext contains a span in between some text(it can be any string), something like this

    This is some <span id="span1"> </span> text
    

    How can I find span from code behind and add user control to it?

  • Anirudh Agarwal
    Anirudh Agarwal over 10 years
    But the point is he is creating that span on runtime. Doing so will also not give
  • Sid M
    Sid M over 10 years
    You are right @AnirudhAgarwal.. Coder i already tried that,it dosen't work.
  • Sid M
    Sid M over 10 years
    well @coder what you answered is the whole process that should be done,it would be really good if you can tell me how it is done?
  • Sid M
    Sid M over 10 years
    you don't need span.InnerHtml = ""; and span.Attributes["id"] = "span"; It works fine without it also,so made the necessary changes.
  • Sid M
    Sid M over 10 years
    when i add a User Control after adding a span as you said, It gets added to my page after text and when you look at the HTML span gets added after This is some Hello text.
  • Mark
    Mark over 10 years
    You might want to add some explanations.