我如何尝试将下面的html代码转换为使用c#的代码,我试过但未给出正确的结果 [英] How to convert the below html code into code behind using c#, i tried but it is not giving proper result

查看:71
本文介绍了我如何尝试将下面的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() + "''".


这篇关于我如何尝试将下面的html代码转换为使用c#的代码,我试过但未给出正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆