datalist嵌套

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

datalist嵌套_第1张图片

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


    
        
<%#DataBinder.Eval(Container.DataItem, "cnvcTitle")%>
<%#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"))%>


--------后台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();
              }
        }



你可能感兴趣的:(asp.net,dataset,string,border,server,table,asp)