我如何尝试将下面的html代码转换为使用c#的代码,我试过但未给出正确的结果

我如何尝试将下面的html代码转换为使用c#的代码,我试过但未给出正确的结果

问题描述:

<h3>
<a href="#"> text</a>
<h3></h3></h3>


 HtmlGenericControl header = new HtmlGenericControl("h3");
 HtmlGenericControl anchor = new HtmlGenericControl("a");
 anchor.Attributes.Add("href", "#");
 anchor.Attributes.Add("InnerText", "'" + k["Question"].ToString() + "'");// i am getting some value from sharepoint custom list which is text here//
                
header.Controls.Add(anchor);


有人可以帮我吗?


can someone help me.

我想您正在Asp.Net中进行此操作.您可以按以下方法尝试.

1)在Aspx中声明Div.
I guess you are doing this in Asp.Net. You may try it as below.

1) Declare Div in your Aspx.
<div id="dvMyHtml" runat="server"></div>



2)在后台代码中



2) In code-behind

StringBuilder sbHtml = new StringBuilder();

sbHtml.Append("<h3>");
sbHtml.Append("<a href="\"#\"">");
sbHtml.Append(Convert.ToString(k["Question"]));
sbHtml.Append("</a>");
sbHtml.Append("<h3></h3></h3>");

dvMyHtml.InnerHtml = sbHtml.ToString();


在PHP中很简单,我们有:
In PHP it is simple and we have:
echo '<h3><a href="#"> text</a><h3></h3></h3>';


但是在asp.net C#代码后面,我们可以使用类似的方式


but in asp.net C# code behind we can use similar way

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<h3><a href="#"> text</a><h3></h3></h3>");
    }
}


输出HTML的最简单方法是像其他两个答案一样直接执行HTML.但是,如果要使用控件,InnerText不是属性,则需要使用 anchor.InnerHtml =''" + k ["Question"].ToString()+''" .
The simplest way to output HTML is to do it directly as in the other two answers. But if you''re going to use controls, InnerText is not an attribute, you need to use anchor.InnerHtml = "''" + k["Question"].ToString() + "''".