伙计我有一个asp:listview项目。我想从数据库中读取数据,并希望对它们进行分类,如下所示。 http://prntscr.com/8rhpcb 现在我的代码如下:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1"
ItemPlaceholderID="iph" >
<ItemTemplate>
<table id="item_table" border="1">
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Label ID="IdLabel" runat="server"
Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="SurnameLabel" runat="server"
Text='<%# Eval("Surname") %>' />
</td>
<td>
<asp:Label ID="SexLabel" runat="server"
Text='<%# Eval("Sex") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server"
Text='<%# Eval("Email") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server"
Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Label ID="AgeLabel" runat="server"
Text='<%# Eval("Age") %>' />
</td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<table id="table1" runat="server" border="1"
style="background-color: #FFFFFF;border-color: #999999;border-style:none;
border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color: #E0FFFF;color: #333333;">
<th id="Th1" style="width:130px" runat="server">Id</th>
<th id="Th2" style="width:130px" runat="server">Name</th>
<th id="Th3" style="width:130px" runat="server">Surname</th>
<th id="Th4" style="width:130px" runat="server">Sex</th>
<th id="Th5" style="width:130px" runat="server">Email</th>
<th id="Th6" style="width:130px" runat="server">City</th>
<th id="Th7" style="width:130px" runat="server">Age</th>
</tr>
<tr>
</tr>
</table>
<table id="table2" runat="server" border="1"
style="background-color: #FFFFFF;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr id="Tr2" runat="server" style="background-color: #E0FFFF;color: #333333;">
<th id="Th8" style="width:130px" runat="server">Id</th>
<th id="Th9" style="width:130px" runat="server">Name</th>
<th id="Th10" style="width:130px" runat="server">Surname</th>
<th id="Th11" style="width:130px" runat="server">Sex</th>
<th id="Th12" style="width:130px" runat="server">Email</th>
<th id="Th13" style="width:130px" runat="server">City</th>
<th id="Th14" style="width:130px" runat="server">Age</th>
</tr>
<tr runat="server" id="iph"></tr>
</table>
</LayoutTemplate>
</asp:ListView>
我可以使用2 listview来做到这一点但我想在1 listview中做到这一点。它可以吗?如果有可能你可以用细节或代码示例解释我吗?
答案 0 :(得分:0)
最后我找到了这样做的方法。代码如下:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1"
ItemPlaceholderID="iph" >
<ItemTemplate>
<table id="item_table" border="1">
<%# AddTitle() %>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Label ID="IdLabel" runat="server"
Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="SurnameLabel" runat="server"
Text='<%# Eval("Surname") %>' />
</td>
<td>
<asp:Label ID="SexLabel" runat="server"
Text='<%# Eval("Sex") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server"
Text='<%# Eval("Email") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server"
Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Label ID="AgeLabel" runat="server"
Text='<%# Eval("Age") %>' />
</td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<table id="table1" runat="server" border="1"
style="background-color: #FFFFFF;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color: #E0FFFF;color: #333333;">
<th id="Th1" style="width:130px" runat="server">Id</th>
<th id="Th2" style="width:130px" runat="server">Name</th>
<th id="Th3" style="width:130px" runat="server">Surname</th>
<th id="Th4" style="width:130px" runat="server">Sex</th>
<th id="Th5" style="width:130px" runat="server">Email</th>
<th id="Th6" style="width:130px" runat="server">City</th>
<th id="Th7" style="width:130px" runat="server">Age</th>
</tr>
<tr runat="server" id="iph">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
并在代码背后:
string lastvalue = "";
protected string AddTitle() {
string newvalue = Eval("Sex").ToString();
if (newvalue != lastvalue)
{
lastvalue = newvalue;
return String.Format("<tr class=" + "group" + "><td colspan=" + "7" + ">Gender:{0}</td></tr>", newvalue);
}
else {
return string.Empty;
}
}
首先,对于像pic这样的数据分组,必须对数据记录进行排序。之后在AddTitle方法中,每次从数据库获取记录时,如果它与以前的性别类型不同,它会控制性别类型,它会添加一列像“男人名单”或“女性名单”这样的表格。我希望它会帮助那些像我一样有问题的人。