DataList嵌套DataList的实现

系统 1722 0
数据服务控件的嵌套最主要的是是内层控件数据的加载和事件的触发。 DataList嵌套的重点是要在外层DataList的ItemDataBound事件中用e.Item.FindControl方法来找到嵌套层 DataList的id,编写嵌套层DataList的绑定数据源事件。下面以两层DataList为例介绍下实现的过程。效果如下图:

---------前台html代码-------

  1. < asp:datalist id = "dlFileType" RepeatColumns = "2" runat = "server" >
  2. < ItemTemplate >
  3. < table border = "0" cellspacing = "0" cellpadding = "0" >
  4. < tr >
  5. < td width = "22%" height = "88" align = "center" valign = "top" >
  6. < img src = '<%#DataBinder.Eval(Container.DataItem,"cnvcPicURL")%>' width = "80" height = "80" >
  7. </ td >
  8. < td valign = "top" >
  9. < table width = "96%" border = "0" cellpadding = "0" cellspacing = "0" >
  10. < tr width = "100%" >
  11. < td colspan = "2" > < img src = '<%#PageBase.strStyleRoot+"/picture/pic_fwzn_08.gif"%>' height = "20" > < %#DataBinder.Eval(Container.DataItem,"cnvcTitle")% >
  12. < asp:Label id = "labFileType" runat = "server" Visible = False Text = '<%#DataBinder.Eval(Container.DataItem,"cniFileTypeID")%>' >
  13. </ asp:Label > </ td >
  14. </ tr >
  15. < tr >
  16. < td width = "300" >
  17. < asp:DataList id = "dlFileList" runat = "server" RepeatColumns = "1" Width = "100%" >
  18. < ItemTemplate >
  19. < TABLE cellSpacing = "1" cellPadding = "1" width = "100%" border = "0" >
  20. < tr >
  21. < td width = "7%" height = "20" align = "center" >
  22. < img src = '<%#PageBase.strStyleRoot+"/picture/pic_fwzn_dot.gif"%>' width = "3" height = "3" > </ td >
  23. < td width = "93%" >
  24. < font color = "#393939" >
  25. < %#GetTitle((string)Convert.ToString(DataBinder.Eval(Container.DataItem,"cnvcTitle")),(string)Convert.ToString(DataBinder.Eval(Container.DataItem,"cnvcFileType")),(string)Convert.ToString(DataBinder.Eval(Container.DataItem,"cniBaseFileID")),(DateTime)DataBinder.Eval(Container.DataItem,"cndtPublishTime"))% >
  26. </ font >
  27. </ td >
  28. </ tr >
  29. </ TABLE >
  30. </ ItemTemplate >
  31. </ asp:DataList >
  32. </ td >
  33. </ tr >
  34. < tr >
  35. < td colspan = "2" bgcolor = "E6E6E6" height = "1" > < img src = '<%#PageBase.strStyleRoot+"/picture/1X1.gif"%>' width = "1" > </ td >
  36. </ tr >
  37. < tr align = "center" >
  38. < td height = "22" colspan = "2" > < a href = "#" title = "可查看到更多相关内容" > < img src = '<%#PageBase.strStyleRoot+"/picture/more.gif"%>' width = "34" height = "11" border = "0" > </ a > </ td >
  39. </ tr >
  40. </ table >
  41. </ td >
  42. </ tr >
  43. </ table >
  44. </ ItemTemplate >
  45. </ asp:datalist >

--------后台cs代码------
内层控件数据绑定与事件声明在外层的ItemDataBind中实现
  1. private void dlFileType_ItemDataBound( object sender,System.Web.UI.WebControls.DataListItemEventArgse)
  2. {
  3. if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
  4. {
  5. DataListdl= null ;
  6. LabellabTypeID= null ;

  7. dl=(DataList)e.Item.FindControl( "dlFileList" )
  8. labTypeID=(Label)e.Item.FindControl( "lbFileType" );
  9. string typeID=labTypeID.Text.ToString();
  10. int iTypeID=Convert.ToInt32(typeID);
  11. string commandText= "select*fromtbfile" ;
  12.     commandText=commandText+ "WhereTypeID=" +iTypeID;
  13. //------------
  14. string connString=ConfigurationSettings.AppSettings[ "dsn" ];
  15. SqlConnectionconn= new SqlConnection();
  16.     conn.ConnectionString=connString;
  17.     conn.Open();
  18.     SqlDataAdaptermyCommand= new SqlDataAdapter(commandText,conn);
  19.     DataSetds= new DataSet();
  20.     myCommand.Fill(ds, "tbFile" );
  21.     conn.Close();
  22. //------------
  23. dl.DataSource=ds.Tables[ "tbFile" ];;
  24. dl.DataBind();
  25. }
  26. }

DataList嵌套DataList的实现


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论