0

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

    }
matty
  • 11
  • 5
  • 1
    Three repeater displays multiple items so there are multiple dropdown lists into it so you can not pick one of them just like that. You need to loop thru the items of repeater control and do FindControl("ddRoles") on each of them. – Chetan Mar 10 '18 at 21:09
  • Reference to which one of the many that are repeated? ... think about it :) – Dusan Mar 10 '18 at 21:52
  • Related: https://stackoverflow.com/questions/19872148/find-label-control-in-repeater-asp-net – wazz Mar 10 '18 at 22:49
  • Hi @Dusan, I have amended the question to, 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? Are you able to help? – matty Mar 12 '18 at 21:21
  • @Chetan Ranpariya, would you be able give an example please? – matty Mar 12 '18 at 21:34
  • @matty please follow the answer of https://stackoverflow.com/questions/6281559/asp-net-repeater-loop-through-items-in-the-item-template let me know if I still need to give you an example.. – Chetan Mar 13 '18 at 01:10
  • Hi @Chetan Ranpariya thank you for the link, I established a reference but I'm unable to obtain the change in value. I've updated my original post with what I've achieved and what I'm hoping to do. I'd be grateful if you could help some more. Thanks – matty Mar 13 '18 at 21:08
  • What value you are trying to obtain? I see that you are trying to print ID of dropdownlist in Debug Windows during ItemDataBound event of repeater. Can you explain what you want to with the dropdownlist of repeater items? What I can understand from the code is you are displaying list of employees in the repeater and there is a dropdownlist of every employee in the repeater. What's next functionality which you want to achieve? – Chetan Mar 14 '18 at 01:51
  • Hi @Chetan Ranpariya I’m looking to extend some code that I have for administering a website. When the user changes the selection I want to change their status to admin or user depending upon what they select. I’ve added some additional code used to populate the repeater and the dropdownlist to my original post. Based on data obtained from the user’s selection I was then hoping to use: Roles.AddUserToRole(username, “admin”); – matty Mar 14 '18 at 22:28

1 Answers1

0

Add a hidden field to the ItemTemplate as follows so that the Name(ID) can be found:

   <ItemTemplate>
   <tr>
    <asp:HiddenField runat="server" ID="Name" Value="<%# Item.Name %>" />

Establish a reference to the repeater line through the TetChanged Event as shown below. Then use FindControl to find the ID and the required value. Update the Role information and then Databind to update the table:

protected void ddRoles_TextChanged(object sender, EventArgs e)
{
    var RepeaterItem = (sender as DropDownList).NamingContainer as RepeaterItem;

    var keyValue = RepeaterItem.FindControl("Name") as HiddenField;

    DataTable data = ViewState["Data"] as DataTable;
    var ddRoles = RepeaterItem.FindControl("ddRoles") as DropDownList;

    Roles.AddUserToRole(keyValue.Value, ddRoles.SelectedValue);
    repeaterManage.DataBind();

}
matty
  • 11
  • 5