页面导航: 首页网络编程ASP.NET实用技巧 → 正文内容 DataList 中动态绑定服务器子控件的代码

DataList 中动态绑定服务器子控件的代码

发布:dxy 字体:[增加 减小] 类型:转载
1、首先绑定dbList (一个DataList控件名称)
2、设置绑定子控件需要的关键字段,即设置dbList的DataKeyField属性。
3、绑定dbList完成以后,循环dbList,使用FindControl方法找到那个控件,然后将一个  
SomeMethod(DataKeyField) 方法返回一个DataReader给子控件。
------------------------------------------------------------------------
       DataSet ds=SqlComd.CreateSqlDataSet(sql,"dstable");
       DataTable dt=ds.Tables[0];
       dbList.DataSource=dt.DefaultView;
       dbList.DataKeyField="userId";
       dbList.DataBind();
            
       for(int i=0; i<dbList.Items.Count; i++)
      {
            int itemIndex=dbList.Items[i].ItemIndex;
            string uid=dbList.DataKeys[itemIndex].ToString();
            //找到这个子控件
            DropDownList drop=(DropDownList)dbList.Items[i].FindControl("dropList");  
            //一个方法,由来根据UID返回一个DataReader
            SqlDataReader dr=GetBrands(uid);     
            drop.DataSource=dr;
            drop.DataTextField="brandnameCn";
            drop.DataValueField="id";
            drop.DataBind();
            dr.Close();       
       }
浏览次数:载入中... 打印本文关闭本文返回首页

文章评论

共有 位脚本之家网友发表了评论我来说两句

同 类 文 章
最 近 更 新
热 点 排 行