[英]How to put server-side control in Literal?
I want put a server-side control in a Literal. 我想将服务器端控件放入文字中。 Is it possible?
可能吗? If yes, how?
如果是,怎么办? I know the description of the class says it all:
我知道该类的描述说明了一切:
Represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server
表示HTML元素,文本和ASP.NET页中不需要在服务器上进行处理的任何其他字符串
If you want to be wrapping server side code in other controls you probably want to be using Panel. 如果要将服务器端代码包装在其他控件中,则可能要使用Panel。 As the comments have already stated, it is impossible to treat literal text as a server side Control.
正如评论所指出的那样,将文本文本视为服务器端控件是不可能的。
Funny this comes up as I just looked through some of my old code yesterday. 有趣的是,昨天我刚浏览了一些旧代码时,出现了这一点。 Literals can do this.
文字可以做到这一点。 I did this originally to emit user controls through a web service call from Javascript(yikes, I know...it was fun) but this will fit your needs as well.
我最初这样做是为了通过Javascript通过Web服务调用发出用户控件(我知道,这很有趣),但这也符合您的需求。
protected void Page_Load(object sender, EventArgs e)
{
litTest.Text = RenderUserControlAsString("WebUserControl1.ascx");
}
private string RenderUserControlAsString(string path)
{
Page page = new Page();
UserControl control = (UserControl)page.LoadControl(path);
//add the control to the page
page.Controls.Add(control);
StringWriter sw = new StringWriter();
HttpContext.Current.Server.Execute(page, sw, true);
//return the rendered markup of the page, which only has our single user control
return sw.ToString();
}
Throw your literal on the page and assign it's text value to RenderUserControlAsString("Path to your User Control"). 将文字放在页面上,然后将其文本值分配给RenderUserControlAsString(“ User Control的路径”)。 With that said, in your user control you must wrap child controls within a form control.
话虽如此,在用户控件中,您必须将子控件包装在窗体控件中。
<form runat="server"> <asp:TextBox id="txtTest" Text="From User Control" runat="server" /> </form>
I hope that helps! 希望对您有所帮助!
TextBox myTextBox = new TextBox();
YourLiteralID.Controls.Add(myTextBox);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.