C# 将数据绑定到控件时出错
我无法通过此代码将数据源链接到中继器C# 将数据绑定到控件时出错,c#,asp.net,data-binding,datasource,C#,Asp.net,Data Binding,Datasource,我无法通过此代码将数据源链接到中继器 protected void Page_Load(object sender, EventArgs e) { //HiddenField used as a placholder HiddenField username = list.FindControl("username") as HiddenField; //list is a DataList containing all of the user names lis
protected void Page_Load(object sender, EventArgs e)
{
//HiddenField used as a placholder
HiddenField username = list.FindControl("username") as HiddenField;
//list is a DataList containing all of the user names
list.DataSource = Membership.GetAllUsers();
list.DataBind();
//Creates a string for each user name that is bound to the datalist
String user = username.Value;
//profilelist is a repeater containing all of the profile information
//Gets the profile of every member that is bound to the DataList
//Repeater is used to display tables of profile information for every user on
// the site in a single webform
profilelist.DataSource = Profile.GetProfile(user);
profilelist.DataBind();
}
我收到了错误信息
An invalid data source is being used for profilelist. A valid data source must implement either IListSource or IEnumerable.
它不起作用的原因是因为
Profile.GetProfile
返回ProfileCommon
。由于错误表明您设置的类型profilelist.Datasource
等于,因此必须是IListSource
或IEnumerable
我建议不要使用中继器,因为您没有实际的重复数据可显示
编辑
我想这就是你想要做的
IEnumerable<ProfileCommon> myProfileList = new IEnumerable<ProfileCommon>();
foreach(var user in userlist)
{
myProfileList.Add(Profile.GetProfile(user));
}
profilelist.datasource = myProfileList;
IEnumerable myProfileList=new IEnumerable();
foreach(userlist中的var用户)
{
添加(Profile.GetProfile(user));
}
profilelist.datasource=myProfileList;
它之所以不起作用,是因为Profile.GetProfile
返回ProfileCommon
。由于错误表明您设置的类型profilelist.Datasource
等于,因此必须是IListSource
或IEnumerable
我建议不要使用中继器,因为您没有实际的重复数据可显示
编辑
我想这就是你想要做的
IEnumerable<ProfileCommon> myProfileList = new IEnumerable<ProfileCommon>();
foreach(var user in userlist)
{
myProfileList.Add(Profile.GetProfile(user));
}
profilelist.datasource = myProfileList;
IEnumerable myProfileList=new IEnumerable();
foreach(userlist中的var用户)
{
添加(Profile.GetProfile(user));
}
profilelist.datasource=myProfileList;
你这样做是不对的。正如艾奇所说,中继器是用来列出事情的。GetProfile不返回列表
最好将控件放在面板中,并在DataBinding事件的“列表”控件中指定它们
换句话说,这里不需要中继器。你的做法是错误的。正如艾奇所说,中继器是用来列出事情的。GetProfile不返回列表 最好将控件放在面板中,并在DataBinding事件的“列表”控件中指定它们
换句话说,这里不需要转发器。我忘了把这篇文章贴上去,但是对于任何需要做类似事情的人来说,这里的代码都很有用
protected void Page_Load(object sender, EventArgs e)
{
List<MembershipUserCollection> usernamelist = new List<MembershipUserCollection>();
usernamelist.Add(Membership.GetAllUsers());
List<ProfileCommon> myProfileList = new List<ProfileCommon>();
foreach (MembershipUser user in usernamelist[0])
{
string username = user.ToString();
myProfileList.Add(Profile.GetProfile(username));
Label emailLabel = profilelist.FindControl("EmailLabel") as Label;
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
List usernamelist=新列表();
添加(Membership.GetAllUsers());
List myProfileList=新建列表();
foreach(用户名列表[0]中的MembershipUser用户)
{
字符串username=user.ToString();
添加(Profile.GetProfile(username));
Label emailLabel=profilelist.FindControl(“emailLabel”)作为标签;
}
}
目前,它显示了大约15个用户名,并提供了链接到这些用户各自配置文件的功能。我忘了发布这篇文章,但对于任何需要进行类似操作的人来说,这里的代码都很有用
protected void Page_Load(object sender, EventArgs e)
{
List<MembershipUserCollection> usernamelist = new List<MembershipUserCollection>();
usernamelist.Add(Membership.GetAllUsers());
List<ProfileCommon> myProfileList = new List<ProfileCommon>();
foreach (MembershipUser user in usernamelist[0])
{
string username = user.ToString();
myProfileList.Add(Profile.GetProfile(username));
Label emailLabel = profilelist.FindControl("EmailLabel") as Label;
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
List usernamelist=新列表();
添加(Membership.GetAllUsers());
List myProfileList=新建列表();
foreach(用户名列表[0]中的MembershipUser用户)
{
字符串username=user.ToString();
添加(Profile.GetProfile(username));
Label emailLabel=profilelist.FindControl(“emailLabel”)作为标签;
}
}
目前,这显示了大约15个用户名,并提供了链接到这些用户各自配置文件的功能。Profile.GetProfile(用户)返回什么?首先,您的问题是什么。其次,为什么要使用中继器来显示配置文件信息?@Etch-这是一种来自角色和成员管理的内置方法,可以获取特定用户的所有配置文件信息user@peroija-我需要知道如何获取绑定到profilelist中继器的Profile.GetProfile(用户)数据。我使用转发器是因为我需要它在HTML方面提供的自由,而且我使用这个UserControl来显示一个表,其中包含站点中每个用户的基本信息。转发器用于对象列表。GetProfile返回一个实例:Profile.GetProfile(用户)返回什么?首先,您的问题是什么。其次,为什么要使用中继器来显示配置文件信息?@Etch-这是一种来自角色和成员管理的内置方法,可以获取特定用户的所有配置文件信息user@peroija-我需要知道如何获取绑定到profilelist中继器的Profile.GetProfile(用户)数据。我使用转发器是因为我需要它在HTML方面提供的自由,而且我使用这个UserControl来显示一个表,其中包含站点中每个用户的基本信息。转发器用于对象列表。GetProfile返回一个实例:关于如何将Profile.GetProfile中的信息存储到IListSource或IEnumberable中的任何细节。中继器是必要的,因为正在为站点的每个用户调用GetProfile,并且他们的所有信息都显示在同一页面上。要将任何一个项转换为
IEnumerable
对象:public IEnumerable GetEnumerable(YourType x){yield return x;}
,但您应该查看应用程序的设计,任何关于如何将Profile.GetProfile中的信息存储到IListSource或IEnumberable中的见解。中继器是必要的,因为正在为站点的每个用户调用GetProfile,并且他们的所有信息都显示在同一页面上。要将任何一个项转换为IEnumerable
对象:public IEnumerable GetEnumerable(YourType x){yield return x;}
,但您应该查看应用程序的设计,不使用此选项。如果我不使用转发器,则如何获取要显示在页面上的GetProfile返回列表?如果我不使用转发器,则如何获取要显示在页面上的GetProfile返回列表?