简体   繁体   English

使用文字控件通过.cs代码实现动态HTML代码

[英]Dynamic HTML code through .cs code using Literal control

I'm trying to generate HTML code from the back-end file (.cs) to produce the output in .aspx. 我正在尝试从后端文件(.cs)生成HTML代码以在.aspx中生成输出。 I'm unable to figure out why when I don't use a method call and directly use Text="HTML", the literal control works perfectly fine with the correct out. 我无法弄清楚为什么当我不使用方法调用而直接使用Text =“ HTML”时,文字控件在正确输出的情况下工作得很好。 However, if I use a method call Text="<%#: methodCall() %>, the output is just a string and it isn't giving the right output. 但是,如果我使用方法调用Text =“ <%#:methodCall()%>,则输出只是一个字符串,并且没有给出正确的输出。

Here's what I mean: 这就是我的意思:

Direct text version: 直接文字版本:

<asp:Literal ID="literal" Mode="PassThrough" runat="server" Text="<b>bold</b><br/><i>italic</i><br/>"/>

Output: 输出:
bold italic 粗体 斜体


Method call version: 方法调用版本:

<asp:Literal ID="literal" Mode="PassThrough" runat="server" Text="<%#: getDetails() %>"/>


Output: 输出:

<b>bold</b><br/><i>italic</i><br/>

Method for this is: 方法是:

public string getDetails()
{
   return "<b>bold</b><br/><i>italic</i><br/>";
}

Expected outcome: 预期结果:
bold 胆大
italic 斜体

Can someone explain to me why this is happening and how I can resolve this? 有人可以向我解释为什么会发生这种情况以及如何解决吗?

Edit: 编辑:
I intend to do data binding so in the end, it would be... 我打算进行数据绑定,所以最终将是...

<%#: getDetails(string, string, string, string) %>

这对您有用吗?

<asp:Literal ID="literal" Mode="PassThrough" runat="server" Text="<%# Server.HtmlEncode(getDetails()) %>"/>

you need to add the following code to the code behind file 您需要将以下代码添加到文件后面的代码中

protected void Page_Load(object sender, EventArgs e)
    {
        this.DataBind(); 
    }

alternatively you might just use the <% =getDetails() %> wherever you want the text instead of using literal control 或者,您可能只在需要文本的地方使用<% =getDetails() %>而不是使用文字控件

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM