当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > DataList 中动态绑定服务器子控件的代码

ASP.NET
c# Random快速连续产生相同随机数的解决方案
form身份验证通过后,只能用FormsAuthentication.RedirectFromLoginPage
ASP.NET Global.asax应用程序文件简介
Asp.net下载功能的解决方案代码
asp.net 生成数字和字母组合的随机数
asp.net显示页面执行时间
DiscuzNT 论坛与主站的同步登录与退出
c# 读取Northwind数据库image字段
asp.net 数据访问层基类
C# 文件保存到数据库中或者从数据库中读取文件
ASP.NET 防止用户跳过登陆界面
asp.net 字符串加密解密技术
asp.net 票据简单应用
基于C# 网站地图制作
asp.net GridView 中增加记录的方法
asp.net 自动将汉字转换成拼音第一个字母
运行asp.net时出现 http错误404-文件或目录未找到
System.Runtime.InteropServices.COMException的解决方法
VS2005 180天限制破解方法
asp.net 面试+笔试题目

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 274 ::
收藏到网摘: n/a

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