[英]ASP.NET Error when sending a list data from code-behind to aspx page
[英]ASP.NET definition list, <dl>, in the code-behind
是否可以在代碼隱藏中創建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>
您可以將元素的標記名稱傳遞給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);
當然,有很多方法可以做到這一點。
1)進行用戶控制。 您的標記可能與“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>
並為UserControl分配數據以適當地呈現文字。
2)創建一個自定義UI控件,然后根據需要呈現該數據。
更有可能的是,選項1將是最簡單的,特別是如果您不想圍繞HTML的輸出進行大量自定義。
使用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.