DataList嵌套DataList

 數據服務控件的嵌套最主要的是是內層控件數據的加載和事件的觸發。 DataList嵌套的重點是要在外層DataList的ItemDataBound事件中用e.Item.FindControl方法來找到嵌套層DataList的id,編寫嵌套層DataList的綁定數據源事件。下面以兩層DataList爲例介紹下實現的過程。效果如下圖:


---------前臺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"%>' width="154" height="20">
                                
<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"><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.DataListItemEventArgs e)
        {
         
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
            {
                  DataList   dl 
= null;
                 Label   labTypeID 
= 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 * from tbfile";
    commandText = commandText + " Where TypeID=" + iTypeID;
    //------------
    string connString = ConfigurationSettings.AppSettings["dsn"];
                 SqlConnection conn = new SqlConnection();
    conn.ConnectionString = connString;
    conn.Open();
    SqlDataAdapter  myCommand = new SqlDataAdapter(commandText,conn);
    DataSet ds = new DataSet();
    myCommand .Fill(ds,"tbFile");
    conn.Close();
    //------------

         
        dl.DataSource = ds.Tables["tbFile"];;
                 dl.DataBind();
              }

        }

如果“labFileType”控件只是爲了傳遞cniFileTypeID值,cs代碼裏獲取typeID的代碼可以改爲:
int iTypeID = (int)DataBinder.Eval(e.Item.DataItem, "cniFileTypeID"); 
 

-------------------------------------------------------------------------------------------------------------------------------------------------

另一形式:


1.html代碼

<HTML>
    
<HEAD>
        
<title>NestedDataLists</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://singlepine.cnblogs.com/">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:DataList runat="server" Id="dlCategories" GridLines="Both" Bordercolor="black" cellpadding="3"
                cellspacing
="0" Headerstyle-BackColor="#DDDDDD" Headerstyle-Forecolor="#777777" Headerstyle-Font-Name="Arial"
                Headerstyle-Font-Size
="14" Headerstyle-Font-Bold="true" Font-Name="Arial" Font-Bold="true"
                Font-Italic
="true" Font-Size="11" ForeColor="Red" RepeatColumns="1">
                
<HeaderTemplate>
                    省市 & 市區
                
</HeaderTemplate>
                
<ItemTemplate>
                    
<%# DataBinder.Eval(Container, "DataItem.province"%>
                    
<br>
                    
<asp:DataList runat="server" Id="ChildDataList" GridLines="None" Bordercolor="black" cellpadding="3" cellspacing="0" Headerstyle-BackColor="#8080C0" Headerstyle-Font-Name="Arial" Headerstyle-Font-Size="8" Font-Name="Arial" Font-Size="8" datasource='<%# DataBinder.Eval(Container, "DataItem.myrelation") %>' RepeatColumns="5">
                        
<ItemTemplate>
                            
&nbsp; &nbsp;
                            
<%# DataBinder.Eval(Container, "DataItem.city"%>
                        
</ItemTemplate>
                    
</asp:DataList>
                
</ItemTemplate>
            
</asp:DataList>
        
</form>
    
</body>
</HTML>

2.cs代碼

public class NestedDataLists : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataList dlCategories;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            
            DataSet ds
=new DataSet();
            
string sql1="select * from province";
            SqlDataAdapter    sda1 
=new SqlDataAdapter(sql1,constring);
            sda1.Fill(ds,
"province");

            
string sql2="select * from city";
            SqlDataAdapter    sda2 
=new SqlDataAdapter(sql2,constring);
            sda2.Fill(ds,
"city");
            
            ds.Relations.Add(
"myrelation", ds.Tables["province"].Columns["provinceID"], ds.Tables["city"].Columns["father"]);
            dlCategories.DataSource
=ds.Tables["province"].DefaultView;
            dlCategories.DataBind();
        }

        
        
Web Form Designer generated code
    }

3.演示數據庫/Files/singlepine/area1.rar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章