In the following code I don’t understand why I can’t establish a reference to the dropdownlist (ddRoles) in the repeater:
<asp:Repeater ItemType="HR_Test_v0_1.Pages.Admin.UserDetails"
ID="repeaterManage"
SelectMethod="GetUsers" runat="server">
<ItemTemplate>
<tr>
<td><%# Item.Name %></td>
<td>
<%# Item.Roles %>
<asp:DropDownList ID="ddRoles"
name="ddRoles"
runat="server"
AppendDataBoundItems="true"
SelectMethod="GetRoles"
SelectedValue="<%# Item.Roles %>"
AutoPostBack="true"
/>
</td>
<td><%# Item.Locked %></td>
<td><%# Item.Online %></td>
<td><button type="submit" name="unlock"
value="<%# Item.Name %>">Unlock</button></td>
<td><button type="submit" name="delete"
value="<%# Item.Name %>">Delete</button></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
DropDownList ddlist = (DropDownList) RepeaterManage
.FindControl("repeaterManage$ddRoles");
Debug.Print(ddlist.UniqueID);
}
}
When I select a different item in the roles ddRoles drop down list, how do I get the new value that the user has selected?
EDIT:
I can now set a reference to the dropdown box in the code below:
private DropDownList ddListControl;
protected void repeaterManage_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
ddListControl = e.Item.FindControl("ddRoles") as DropDownList;
Debug.Print(string.Format("New value: {0}", ddListControl.ID));
}
This gets a reference, but doesn't give me the value that the user has supplied. What I really want to do is detect the change in the value of the dropdownlist in the Page_Load event and then do something in response to the change.
Additional code:
public IEnumerable<UserDetails>GetUsers()
{
return Membership.GetAllUsers()
.Cast<MembershipUser>().Select(m => new UserDetails
{
Name = m.UserName,
Roles = string.Join(",", Roles.GetRolesForUser(m.UserName)),
Locked = m.IsLockedOut,
Online = m.IsOnline
});
}
public IEnumerable<string>GetRoles()
{
//Get a list of all roles
return Roles.GetAllRoles();
}