您的位置:首页 > 其它

DataList嵌套终于成功_07.7.25感谢csdn上的朋友

2007-07-25 14:21 381 查看
两个表tb_newsKind,tb_article,表1存的是栏目类别kindID,kindName依次存储的是栏目的编号和名称,如34是新闻播报,其子栏目为35时事新闻和36整点新闻两者的parentID都为34)。
页面接收传入的一级栏目的kindID来调用它的二级栏目DataList1一行两列的循环显示kindName(kindID)已经实现,问题嵌套的DataList2需要根据DataList1获取的kindID来读取tb_article表中的新闻(tb_article中typeID为nvarchar型比如值为 |34|35|,表示该条记录是属于新闻播报栏目下时事新闻)以列表形式显示在对应的栏目下如:
新闻播报(35) 时事新闻(36)
1.** 1.**
2.** 2.**
一行两列的循环,新闻播报下面的1,2记录根据35从tb_article表中筛选typeID like '%35%' 得到!
cs页面中现只绑定好了DataList1的数据源,请问DataList2该如何接收DataList1的kindID的值进行绑定??

正确代码如下:
aspx代码如下: 最主要的是看子DataList的DataSource事件的绑定
<!--主DataList开始-->
<asp:DataList ID="DataList1" runat="server" DataKeyField = "kindId" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" CellPadding="0" RepeatDirection="Horizontal" RepeatColumns="2">
<ItemTemplate>
<!--tb1 start--><table width="212" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<table width="212" height="200" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="36" valign="bottom" background="images/cunct_1_01.gif"><table width="262" height="25" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td width="102"><div align="center" class="f">
<asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.kindName") %>'></asp:Label>
</div></td>
<td width="101"></td>
<td width="59"><div align="center"><a href='<%#"more.aspx?typeid="+DataBinder.Eval(Container.DataItem,"kindId") %>' class="bb" target="_self"><img src="images/cunct_1_more.gif" width="33" height="10" border="0"></a></div></td>
</tr>
</table></td>
</tr>
<tr>
<td height="158" valign="top" background="images/cunct_1_02.gif">
<!--1start--><table width="270" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" style="height: 10px"></td>
</tr>
<tr>
<td colspan="5" valign="top" style="padding-left:10px;"> /*一定要注意这个<td>不小心网页布局就会坏了*/
<asp:DataList ID="DataList2" runat="server" CellPadding="1" CellSpacing="3" RepeatDirection="Vertical" RepeatColumns="1" DataSource='<%# GetTitleID(DataBinder.Eval(Container.DataItem, "kindId").ToString()) %>'>
<ItemTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="16" height="20"><div align="center"><img src="images/shipin_dian.gif" width="4" height="4" /></div></td>
<td width="280" height="20"><a href='<%#"info.aspx?id="+DataBinder.Eval(Container.DataItem,"newID") %>' class="bb" target="_blank"><asp:Label ID="Label3" runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.title") %>'></asp:Label></a></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td></tr>
</table><!--1end-->
</td>
</tr>
<tr>
<td height="6" background="images/shipin_z01.gif"><img src="images/cunct_1_03.gif" width="290" height="20" /></td>
</tr>
</table>
</td>
</tr>
</table><!--tb1 end-->
</ItemTemplate>
</asp:DataList><!--主DataList结束-->

cs页面代码如:
public DataView GetTitleID(string kindId)
{
SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=db_nxt");
SqlDataAdapter da = new SqlDataAdapter("select top 6 * from tb_article where TypeId like '%'+@kindId+'%' ",cn);
da.SelectCommand.Parameters.Add("@kindId", SqlDbType.VarChar, 11).Value = kindId;
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0].DefaultView;
}

Page_Load里面不用写这个绑定的相关调用,大功造成了。谢谢大家,这个问题拖了我3天不已啊……今天突然又看了看朋友给我回的贴子,做出来了。呵呵
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: