2011-03-09 44 views
0

我有嵌套datalist我有两个datalist第一个获取类别和第二个子类别我做我的代码很好,第一个数据列表很好地得到类别,但第二个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"> 
       &nbsp;</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"> 
        &nbsp;</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 [email protected] 

回答

1

我觉得你只是返回一个空DataTable

尝试

public DataTable GetSubByCategory(string ID) 
{ 
    return cls.GetSubCategory(ID); 
}