datalist嵌套datalist,子datalist数据不显示
想的到的效果,是父datalist显示新闻的大类,子datalist显示新闻大类下新闻的标题。
但是现在父datalist已经显示出来,但是子datalist的数据不显示。 --------------------编程问答-------------------- 程序前台程序
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" width= "100%">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" OnDataBinding="DataList1_DataBinding" OnItemDataBound="DataList1_ItemDataBound" CellPadding="4" ForeColor="#333333" Height="226px" Width="421px" BorderColor="Blue">
<ItemTemplate>
<table width= "100%" >
<tr>
<td>
<asp:Label runat="server" id="text1" Text=""><%# Eval("C_TypeName")%></asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("I_TypeID") %>' Visible=False></asp:Label>
</td>
</tr>
<ItemTemplate></ItemTemplate>
<asp:DataList ID="DataList2" runat="server" RepeatLayout="Flow">
<ItemTemplate>
<table width= "100%" >
<tr>
<td> <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"C_Name")%>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></table>
</ItemTemplate>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</form>
</body>
</html>
--------------------编程问答-------------------- 后台程序:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Default2 : System.Web.UI.Page
{
protected string connString;
protected void Page_Load(object sender, EventArgs e)
{
connString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Request.PhysicalApplicationPath + "DataList.mdb";
if (!Page.IsPostBack)
{
BindData();
//BindArticle();
}
}
public void BindData()
{
string sql = "select * from articletype";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = connString;
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter(sql,con);
DataSet ds = new DataSet();
da.Fill(ds,"articletype");
con.Close();
DataList1.DataSource = ds.Tables["articletype"].DefaultView;
DataList1.DataBind();
}
protected void DataList1_DataBinding(object sender, EventArgs e)
{
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataList dl=null;
Label l1 = null;
l1=(Label)e.Item.FindControl("Label1");
string typeID = l1.Text.ToString();
dl = (DataList)e.Item.FindControl("DataList2");
string sql1 = " select * from Article ";
sql1 = sql1 + " Where (I_TypeID = '" + typeID+ "')";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbDataAdapter da1 = new OleDbDataAdapter(sql1,conn);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "Article");
conn.Close();
dl.DataSource = ds1.Tables["Article"].DefaultView;
dl.DataBind();
}
}
--------------------编程问答-------------------- 后台代码也写出来看看 --------------------编程问答-------------------- 在事件里绑定嵌套的DataList没做过。
我做的时候是单独绑定嵌套的DataList,没什么问题,建议参考下... --------------------编程问答-------------------- 请参考影片:
DataList include DataList.wmv --------------------编程问答-------------------- 先看看
不能解决再来请教
--------------------编程问答-------------------- <table align="center" border="0" cellpadding="0" cellspacing="0" class="TableStyle">
<tr>
<td>
<asp:DataList ID="dltBBSType" runat="server" Width="100%" OnItemDataBound="dltBBSType_ItemDataBound">
<ItemTemplate>
<table border="1" bordercolor="black" style="border-style:none" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="5" height="25px" background="../Images/MovBI2.gif" class="FS">
<%#Eval("TypeName")%>
</td>
</tr>
<tr class="TDFontStyle" height="30px">
<td width="50px" style="border-right-width:0;border-bottom-width:0"> </td>
<td width="430px" style="border-right-width:0;border-bottom-width:0">
论坛
</td>
<td width="80px" align="center" style="border-right-width:0;border-bottom-width:0">主题</td>
<td width="80px" align="center" style="border-right-width:0;border-bottom-width:0">帖数</td>
<td width="150px" align="center" style="border-bottom-width:0">版主</td>
</tr>
<tr>
<td colspan="5">
<asp:DataList ID="dltBBSTitle" runat="server" Width="100%">
<ItemTemplate>
<table border="1" bordercolor="black" cellpadding="0" cellspacing="0" style="border-style:none" width="100%">
<tr>
<td width="50px" height="20px"> </td>
<td width="430px"><%#Eval("TitleName")%></td>
<td width="80px"> </td>
<td width="80px"> </td>
<td width="150px"> </td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = BBSBF.SelBBSTypes();
dltBBSType.DataSource = dt;
dltBBSType.DataKeyField = "TypeID";
dltBBSType.DataBind();
}
}
protected void dltBBSType_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataList dltBBSTitle = e.Item.FindControl("dltBBSTitle") as DataList;
DataTable dt = BBSBF.SelBBSTypes();
DataTable dt2 = BBSBF.SelBBSTitleForType(Convert.ToInt32(dltBBSType.DataKeys[e.Item.ItemIndex]));
dltBBSTitle.DataSource = dt2;
dltBBSTitle.DataBind();
}
我这个是论坛的大标题和所属大标题的小标题,LZ参考下 --------------------编程问答-------------------- mark.
补充:.NET技术 , ASP.NET