我在aspx中编写了以下代码,问题是我需要在C#后面的代码中创建相同的datalist:
<asp:DataList ID="DataList1" runat="server">
<HeaderTemplate>
<tr>
<th style="background-color: Black;">
<div style="color: White; font-size: medium; padding: 0; margin: 0;">
TEST</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
date</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
Buy/Sell</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
Call/Put</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
name</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
amount</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
price1</div>
</th>
<th>
<div style="background-color: #ADAAB1; color: #E3E2E7; padding: 5px; font-size: x-small;">
price2</div>
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<div>
<tr>
<td>
<div style="background-color: #71B24C; color: White; height: 50px; text-align: center;">
<%# Eval("option")%></div>
</td>
<td>
<div style="background-color: #ADAAB1; color: White; height: 50px; padding-right: 5px;
font-size: medium; text-align: center;">
<%# Eval("date")%></div>
</td>
<td>
<div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
text-align: center;">
<%# Eval("type")%></div>
</td>
<td>
<div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
text-align: center;">
<%# Eval("action")%></div>
</td>
<td>
<div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
text-align: center;">
<%# Eval("pos_name")%></div>
</td>
<td>
<div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
text-align: center;">
<%# Eval("amount")%></div>
</td>
<td>
<div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
text-align: center;">
<%# Eval("unitPrice")%></div>
</td>
<td>
<div style="background-color: #D9D9D3; padding-right: 5px; height: 50px; font-size: medium;
text-align: center;">
<%# Eval("total")%></div>
</td>
</tr>
</div>
</ItemTemplate>
</asp:DataList>
我的意思是我演示的代码在aspx页面中完美运行。我很有兴趣获得相同的效果(显示datalist),但通过后面的代码。我的意思是在代码后面编写代码:DataList = new DataList();我的问题是,我不知道如何在代码中使用HeaderTemplate。 如果你能提供示例代码,那就太好了!
答案 0 :(得分:1)
后面的代码中的HeaderTemplate必须是ITemplate类型。有关创建模板的信息,请参阅这些文章。
http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate(v=VS.85).aspx
希望他们可以提供帮助。
答案 1 :(得分:0)
我对DataLists没有任何经验,但从查看the MSDN documentation看起来你想要做的就是将你的数据源绑定到代码隐藏中的DataList然后使用asp控件在aspx页面上显示绑定数据。
因此,您需要将数据绑定到列表中:
using (conn = new SqlConnection(cString)) {
conn.Open();
comm = new SqlCommand(selString, conn);
DataList1.DataSource = comm.ExecuteReader();
DataList1.DataBind();
}
然后格式化您的DataList以加载来自绑定源的数据,就像您已经完成的那样,使用<%# Eval("date")%>
之类的语句。
MSDN在how to create ASP.NET Server controls上还有一个页面:
1.在.aspx文件中,在控件中插入一个元素以标识要创建的模板,如以下示例所示:<asp:DataList id="DataList1" runat="server"> <ItemTemplate>2.在模板元素内,添加HTML文本和其他控件作为模板的内容。使用常规语法包含嵌入控件的属性和数据绑定值,如以下示例所示:
</ItemTemplate> </asp:DataList><asp:DataList id="DataList3" runat="server"> <ItemTemplate> Name: <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'/> </ItemTemplate> </asp:DataList>3.对要创建的每个模板重复步骤1和2。