我有嵌套的datalist我有两个datalist第一个用于获取gategories而第二个子类别用于类别我很好地完成了我的代码并且第一个数据列表很好地获得了类别,但第二个数据列表没有得到任何数据并且没有找到错误所以请任何一个人帮帮我。
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>
CS CODE
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;
}
}
PROC
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
答案 0 :(得分:1)
我想你只是回复一个空白的DataTable
。
尝试
public DataTable GetSubByCategory(string ID)
{
return cls.GetSubCategory(ID);
}