在下面的代码中,我不明白为什么我无法在转发器中建立对下拉列表(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);
}
}
当我在角色ddRoles下拉列表中选择其他项目时,如何获取用户选择的新值?
编辑:
我现在可以在下面的代码中设置对下拉框的引用:
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();
}
答案 0 :(得分:0)
为ItemTemplate添加一个隐藏字段,如下所示,以便找到名称(ID):
<ItemTemplate>
<tr>
<asp:HiddenField runat="server" ID="Name" Value="<%# Item.Name %>" />
通过TetChanged事件建立对转发器行的引用,如下所示。然后使用FindControl查找ID和所需的值。更新角色信息,然后更新Databind以更新表:
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();
}