DataList嵌套DataList的實現

來源:互聯網
上載者:User
        資料服務控制項的嵌套最主要的是是內層控制項資料的載入和事件的觸發。 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"%>'  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.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();
              }
        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.