在 asp.net 4.5 中的中继器中建立对 DropDownList 的引用

Establish A Reference To A DropDownList Within A Repeater In asp.net 4.5

在下面的代码中我不明白为什么我不能在转发器中建立对下拉列表(ddRoles)的引用:

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

后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        {

            DropDownList ddlist = (DropDownList) RepeaterManage
                      .FindControl("repeaterManage$ddRoles");

            Debug.Print(ddlist.UniqueID);

        }
    }

当我 select 角色 ddRoles 下拉列表中的不同项目时,如何获取用户 selected 的新值?

编辑:

我现在可以在下面的代码中设置对下拉框的引用:

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

    }

这得到了一个参考,但没有给我用户提供的值。我真正想做的是检测 Page_Load 事件中下拉列表值的变化,然后根据变化做一些事情。

附加代码:

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

    }

如下在ItemTemplate中添加一个隐藏字段,以便可以找到Name(ID):

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

通过TetChanged 事件建立对转发器线路的引用,如下所示。然后使用 FindControl 查找 ID 和所需的值。更新 Role 信息,然后 Databind 更新 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();

}