數據服務控件的嵌套最主要的是是內層控件數據的加載和事件的觸發。 DataList嵌套的重點是要在外層DataList的ItemDataBound事件中用e.Item.FindControl方法來找到嵌套層 DataList的id,編寫嵌套層DataList的綁定數據源事件。下面以兩層DataList為例介紹下實現的過程。效果如下圖:
---------前臺html代碼-------
--------后臺cs代碼------
內層控件數據綁定與事件聲明在外層的ItemDataBind中實現
---------前臺html代碼-------
- < asp:datalist id = "dlFileType" RepeatColumns = "2" runat = "server" >
- < ItemTemplate >
- < table border = "0" cellspacing = "0" cellpadding = "0" >
- < tr >
- < td width = "22%" height = "88" align = "center" valign = "top" >
- < img src = '<%#DataBinder.Eval(Container.DataItem,"cnvcPicURL")%>' width = "80" height = "80" >
- </ td >
- < td valign = "top" >
- < table width = "96%" border = "0" cellpadding = "0" cellspacing = "0" >
- < tr width = "100%" >
- < td colspan = "2" > < img src = '<%#PageBase.strStyleRoot+"/picture/pic_fwzn_08.gif"%>' height = "20" > < %#DataBinder.Eval(Container.DataItem,"cnvcTitle")% >
- < asp:Label id = "labFileType" runat = "server" Visible = False Text = '<%#DataBinder.Eval(Container.DataItem,"cniFileTypeID")%>' >
- </ asp:Label > </ td >
- </ tr >
- < tr >
- < td width = "300" >
- < asp:DataList id = "dlFileList" runat = "server" RepeatColumns = "1" Width = "100%" >
- < ItemTemplate >
- < TABLE cellSpacing = "1" cellPadding = "1" width = "100%" border = "0" >
- < tr >
- < td width = "7%" height = "20" align = "center" >
- < img src = '<%#PageBase.strStyleRoot+"/picture/pic_fwzn_dot.gif"%>' width = "3" height = "3" > </ td >
- < td width = "93%" >
- < font color = "#393939" >
- < %#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"))% >
- </ font >
- </ td >
- </ tr >
- </ TABLE >
- </ ItemTemplate >
- </ asp:DataList >
- </ td >
- </ tr >
- < tr >
- < td colspan = "2" bgcolor = "E6E6E6" height = "1" > < img src = '<%#PageBase.strStyleRoot+"/picture/1X1.gif"%>' width = "1" > </ td >
- </ tr >
- < tr align = "center" >
- < td height = "22" colspan = "2" > < a href = "#" title = "可查看到更多相關內容" > < img src = '<%#PageBase.strStyleRoot+"/picture/more.gif"%>' width = "34" height = "11" border = "0" > </ a > </ td >
- </ tr >
- </ table >
- </ td >
- </ tr >
- </ table >
- </ ItemTemplate >
- </ asp:datalist >
--------后臺cs代碼------
內層控件數據綁定與事件聲明在外層的ItemDataBind中實現
- private void dlFileType_ItemDataBound( object sender,System.Web.UI.WebControls.DataListItemEventArgse)
- {
- if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
- {
- DataListdl= null ;
- LabellabTypeID= null ;
-
- dl=(DataList)e.Item.FindControl( "dlFileList" )
- labTypeID=(Label)e.Item.FindControl( "lbFileType" );
- string typeID=labTypeID.Text.ToString();
- int iTypeID=Convert.ToInt32(typeID);
- string commandText= "select*fromtbfile" ;
- commandText=commandText+ "WhereTypeID=" +iTypeID;
- //------------
- string connString=ConfigurationSettings.AppSettings[ "dsn" ];
- SqlConnectionconn= new SqlConnection();
- conn.ConnectionString=connString;
- conn.Open();
- SqlDataAdaptermyCommand= new SqlDataAdapter(commandText,conn);
- DataSetds= new DataSet();
- myCommand.Fill(ds, "tbFile" );
- conn.Close();
- //------------
- dl.DataSource=ds.Tables[ "tbFile" ];;
- dl.DataBind();
- }
- }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
