数据绑定到控件时出错

时间:2012-05-02 02:02:19

标签: 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
    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.

3 个答案:

答案 0 :(得分:2)

它之所以不起作用,是因为Profile.GetProfile返回ProfileCommon。由于错误指出您设置的profilelist.Datasource等于的类型,因此必须为IListSourceIEnumerable

我建议您不要使用转发器,因为您没有要显示的实际重复数据。

修改

我认为这就是你想要做的。

        IEnumerable<ProfileCommon> myProfileList = new IEnumerable<ProfileCommon>();

        foreach(var user in userlist)
        {
             myProfileList.Add(Profile.GetProfile(user));
        }

        profilelist.datasource = myProfileList;

答案 1 :(得分:1)

你的错误。正如Etch所说,转发器用于列出的东西。 GetProfile不会返回列表。

最好只将控件放在一个面板中,并在“list”控件的数据绑定事件中指定它们。

换句话说,你在这里不需要转发器。

答案 2 :(得分:0)

我忘了发帖了,但是对于那些需要做类似事情的人来说,背后的代码是有效的

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

目前,这显示了大约15个用户名,并提供了链接到每个用户各自配置文件的功能。