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);
Author by
Sid M
Updated on June 28, 2022Comments
-
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 inPageContent
div, mytext contains a span in between some text(it can be any string), something like thisThis is some <span id="span1"> </span> text
How can I find span from code behind and add user control to it?
-
Anirudh Agarwal over 10 yearsBut the point is he is creating that span on runtime. Doing so will also not give
-
Sid M over 10 yearsYou are right @AnirudhAgarwal.. Coder i already tried that,it dosen't work.
-
Sid M over 10 yearswell @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 over 10 yearsyou don't need
span.InnerHtml = "";
andspan.Attributes["id"] = "span";
It works fine without it also,so made the necessary changes. -
Sid M over 10 yearswhen 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 over 10 yearsYou might want to add some explanations.