如何将服务器端控制放入 Literal 中?

发布于 2025-01-01 15:38:18 字数 133 浏览 2 评论 0原文

我想将服务器端控件放在文字中。是否可以?如果是,怎么办?我知道课程的描述说明了一切:

表示 ASP.NET 中的 HTML 元素、文本和任何其他字符串 不需要服务器处理的页面

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

套路撩心 2025-01-08 15:38:18

如果您想将服务器端代码包装在其他控件中,您可能需要使用面板。正如评论已经指出的,不可能将文字文本视为服务器端控件。

If you want to be wrapping server side code in other controls you probably want to be using Panel. As the comments have already stated, it is impossible to treat literal text as a server side Control.

逆光下的微笑 2025-01-08 15:38:18

有趣的是,当我昨天查看一些旧代码时,就出现了这个问题。文字可以做到这一点。我这样做最初是为了通过 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();
    }

将文字放在页面上,并将其文本值分配给 RenderUserControlAsString(“用户控件的路径”)。话虽如此,在您的用户控件中,您必须将子控件包装在表单控件中。

<form runat="server"> <asp:TextBox id="txtTest" Text="From User Control" runat="server"  /> </form>

我希望这有帮助!

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.

    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"). 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!

深居我梦 2025-01-08 15:38:18
TextBox myTextBox = new TextBox();
YourLiteralID.Controls.Add(myTextBox);
TextBox myTextBox = new TextBox();
YourLiteralID.Controls.Add(myTextBox);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文