简体   繁体   English

ASP.NET定义列表, <dl> ,在代码隐藏中

[英]ASP.NET definition list, <dl>, in the code-behind

Is it possible to create a HTML definitions list in the code-behind? 是否可以在代码隐藏中创建HTML定义列表? I'm trying to programmatically generate the following HTML. 我正在尝试以编程方式生成以下HTML。

<dl style="overflow: hidden; font-size: small;">
    <dt style="float: left; width: 200px; clear: both; text-align: right; margin-left: 15px;">Apple:</dt>
    <dd style="float: left; width: 90px; margin: 0px 0px 8px;">Fruit<br>Red<br></dd>
</dl>

You can pass the element's tag name to the HtmlGenericControl constructor: 您可以将元素的标记名称传递给HtmlGenericControl构造函数:

HtmlGenericControl dl = new HtmlGenericControl("dl");
dl.Attributes.Add("style", "overflow: hidden; font-size: small;");
Page.Controls.Add(dl);

HtmlGenericControl dt = new HtmlGenericControl("dt");
dt.Attributes.Add("style", "float: left; width: 200px; etc.");
dt.InnerText = "Apple:";
dl.Controls.Add(dt);

Sure, there are many ways of doing this. 当然,有很多方法可以做到这一点。

1) Make a User Control. 1)进行用户控制。 Your markup could be as simple as the following in your "DataDefinition.ascx" control: 您的标记可能与“DataDefinition.ascx”控件中的以下内容一样简单:

<dl style="overflow: hidden; font-size: small;">
   <dt style="float: left; width: 200px; clear: both; text-align: right; margin-left: 15px;"><asp:Literal ID="literalIdentifier" runat="server" /></dt>
   <dd style="float: left; width: 90px; margin: 0px 0px 8px;"><asp:Literal ID="literalDefinition" runat="server" /><br>Red<br></dd>
</dl>

And assign your data for the UserControl to render appropriately to the literals. 并为UserControl分配数据以适当地呈现文字。

2) Make a custom UI control that renders that data however you want. 2)创建一个自定义UI控件,然后根据需要呈现该数据。

More than likely, option 1 is going to be the easiest, especially if you aren't wanting to make a lot of customization around the output of the HTML. 更有可能的是,选项1将是最简单的,特别是如果您不想围绕HTML的输出进行大量自定义。

Use a stringbuilder 使用stringbuilder

StringBuilder sb = new StringBuilder();

sb.Append(@"<dl style='overflow: hidden; font-size: small;">
                <dt style='float: left; width: 200px; clear: both; text-align: right; margin-left: 15px;'>Apple:</dt>
                <dd style='float: left; width: 90px; margin: 0px 0px 8px;'>Fruit<br>Red<br></dd>
            </dl>");

myControl.innerHtml = sb.toString();

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

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