ASP.NET definition list, <dl>, in the code-behind
Is it possible开发者_StackOverflow社区 to create a HTML definitions list in the code-behind? I'm trying to programmatically generate the following 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 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. Your markup could be as simple as the following in your "DataDefinition.ascx" control:
<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.
2) Make a custom UI control that renders that data however you want.
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.
Use a 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();
精彩评论