如何将所有用户的DropDownlist添加到ListView



我有一个ListView控件,我想编辑InsertItem模板以包括一个Dropdownlist Item,它包含Membership Directory中的用户列表。

我试过这个:

MembershipUserCollection users = Membership.GetAllUsers();
assigned_volunteerDropDownBox.DataSource = users;
assigned_volunteerDropDownBox.DataBind();

最大的问题是,它说assigned_volunteerDropDownBox在当前环境中不存在。。。事实上,插入模板上的TextBox项都不在当前上下文中(这正常吗)?

我该怎么做?我希望能够只允许使用有效的用户名更新行。

我看了一下,但它实际上并不包含将DataBind绑定到DropDownList项的代码。

您可以从代码后面访问assigned_volunteerDropDownBox;但从下拉控件本身进行绑定可能更容易。创建要绑定到的属性:

// lazy-loaded property
public MembershipUserCollection UsersCollection
{
get
{ 
if (_usersCollection == null)
{
_usersCollection = Membership.GetAllUsers();
}
return _usersCollection;
}
}
MembershipUserCollection _usersCollection;

然后在DropDownList:上使用DataSource

<asp:DropDownList RunAt="Server" DataSource=<%# UsersCollection %> ID="assigned_volunteerDropDownBox" />

您需要使用FindControl在模板中查找控件。

最新更新