我有嵌套的 datalist 我有两个 datalist 第一个用于获取门,第二个用于类别的子类别.
ASP
<div>
<asp:DataList ID="dlCategory" runat="server">
<EditItemStyle ForeColor="#CC3300" />
<AlternatingItemStyle ForeColor="#CC3300" />
<ItemStyle ForeColor="#CC3300" />
<SelectedItemStyle ForeColor="#CC3300" />
<HeaderTemplate>
<div class="buttn_hed_red">
</div>
<div class="buttn_hed_bg">
<div class="lm7 tm1 buttn_hed_txt">
Projectors</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="buttn_div">
<div class="buttn_red_sqr">
</div>
<div class="lm5 tm2 buttn_txt">
<a href='<%#Eval("ID","Category.aspx?ID={0}") %>' class="buttn_txt">
<asp:Label ID="LblCat" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Category") %>'></asp:Label>
</a>
</div>
</div>
<asp:DataList ID="dlSubCategory" runat="server" DataSource='<%# GetSubByCategory(Convert.ToString(Eval("ID")))%>'>
<EditItemStyle ForeColor="#CC3300" />
<AlternatingItemStyle ForeColor="#CC3300" />
<SelectedItemStyle ForeColor="#CC3300" />
<ItemTemplate>
<div class="buttn_div_sub">
<div class="lm40 tm2 buttn_txt">
<asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SubCategory") %>'></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
客户代码
public DataTable GetSubByCategory(string ID)
{
DataTable dt = new DataTable();
cls.GetSubCategory(ID);
return dt;
}
方法
public DataTable GetSubCategory(string Category_Id)
{
using
(SqlConnection conn = Connection.GetConnection())
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_GetParentByCategoryID";
SqlParameter ParentID_Param = cmd.Parameters.Add("@CategoryID", SqlDbType.Int);
ParentID_Param.Value = Category_Id;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
}
程序
ALTER proc [dbo].[SP_GetParentByCategoryID]
(
@CategoryID int
)
as
select Cat2.[Name] as "SubCategory" ,Cat2.ParentID
from Categories Cat1
inner join Categories Cat2
ON Cat1.ID=Cat2.ParentID
where Cat2.ParentID=@CategoryID