简体   繁体   English

如何从代码后面找到span并为其添加用户控件?

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

I've a div 我有一个div

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

Now I'm adding HTML to it from code behind(c#) like this 现在我正在从代码隐藏(c#)这样添加HTML

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 其中mytext是我要在PageContent div中添加的内容,mytext包含一些文本之间的跨度(可以是任何字符串),类似这样的东西

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

How can I find span from code behind and add user control to it? 如何从代码后面找到span并为其添加用户控件?

//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);

解析/提取span1mytext ,创建HtmlGenericControl这个解析信息,将其添加到PageContent格,然后尝试添加用户控件这个span

 var span = new HtmlGenericControl("span");
 span.InnerHtml = "Hello ";
 span.Attributes["id"] = "span";
 span.ID = "span";
 String myTesxt = @"This is some " + span.InnerHtml + " text";
 myDiv.InnerHtml = myTesxt;
 span.InnerHtml = "";

 myDiv.Controls.Add(span);
 var a=myDiv.FindControl("span");

Try adding span like this in your Div then you will find it in your code behind. 尝试在Div中添加这样的跨度,然后您将在后面的代码中找到它。 Try this. 试试这个。 Hopefully this will solve your purpose. 希望这将解决您的目的。

You can add attributes in the span according to your need. 您可以根据需要在跨度中添加属性。

To add pure tag of the span with the back code please as following code 要使用后面的代码添加跨度的纯标记,请按以下代码进行操作

    StringBuilder sb = new StringBuilder();
    sb.Append("<script>");            
    sb.Append("alert('aa')");
    sb.Append("</script>");
    HtmlGenericControl span = new HtmlGenericControl("span");
    span.InnerHtml = sb.ToString();
    this.form1.Controls.Add(span);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从后面的代码访问和设置跨度控件的属性? - How can I access and set the properties of span control from code behind? 如何找到面板并通过其背后的代码对其添加控件 - How to find a panel and add control to it from it's code behind 如何在代码中包装我的用户控件? - How can I wrap my user control in code behind? 如何使用XAML和后面的代码序列化WPF用户控件 - How can I serialize wpf user control with xaml and code behind 如何从后面的代码刷新自定义WPF用户控件? - How can I refresh a custom wpf user control from code behind? 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 使用 NavigationView 控件,如何从后面的代码导航? - With a NavigationView control, how can I navigate from the code behind? 如何通过使用后面的代码在Gridview的itemtemplate内的控件上添加只读属性? - How can I add readonly attribute at a control inside itemtemplate of Gridview by using code behind? 如何从用户控件页面后面的代码在ListView的ItemTemplate中找到控件? - how to find control in ItemTemplate of the ListView from code behind of the usercontrol page? 如何在C#之后的代码中嵌入用户控件(ascx)? - How to embed user control (ascx) from code behind C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM