如何从ListView中的行获取所选的DDL值

时间:2015-03-07 16:36:35

标签: c# asp.net listview asp.net-identity

现在我只是想在ASP.NET中组合一个简单的角色分配页面,我创建了一个listview,在我的项目模板中,我有一个下拉列表,在同一个模板中有一个按钮。 DDL连接到ODS,它获取当前角色,我有一个方法,它将接受角色和用户名,并将该用户分配给特定角色。我的代码如下:

<h2>Users</h2>
<asp:ListView ID="UserListView" runat="server"
ItemType="Synergy_System.Entities.Security.ApplicationUser"
OnItemCommand="UserListView_ItemCommand">
<EmptyDataTemplate>
    <table runat="server">
        <tr>
            <td>
                No users in this site.
                <asp:LinkButton runat="server" CommandName="AddUsers" Text="Add  users" ID="AddUsersButton" />
            </td>
        </tr>
    </table>
</EmptyDataTemplate>
<ItemTemplate>
    <tr>
        <td>
            <asp:Label Text='<%# Item.UserName %>' runat="server" ID="UserNameLabel" /></td>
        <td>
            <asp:Label Text='<%# Item.Email %>' runat="server" ID="EmailLabel" /></td>
        <td><em>password is hashed</em></td>

        <td>
            <asp:DropDownList ID="RolesList" runat="server" DataSourceID="RoleListODS" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
            <asp:Button ID="AddRoleToUser" runat="server" Text="Add Role" onclick="AddRoleToUser_Click" />
        </td>
    </tr>
</ItemTemplate>
<LayoutTemplate>
    <table runat="server">
        <tr runat="server">
            <td runat="server">
                <table runat="server" id="itemPlaceholderContainer"
                    class="table table-condensed table-hover table-striped">
                    <tr runat="server">
                        <th runat="server">User Name</th>
                        <th runat="server">Email</th>
                        <th runat="server">Password</th>

                        <th runat="server">Roles</th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder"></tr>
                </table>
            </td>
        </tr>
        <tr runat="server">
            <td runat="server">
                <asp:DataPager runat="server" ID="DataPager1">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True"></asp:NextPreviousPagerField>
                    </Fields>
                </asp:DataPager>
            </td>
        </tr>
    </table>
</LayoutTemplate>

我的代码背后几乎是空的,因为它现在就像这样:

protected void AddRoleToUser_Click(object sender, EventArgs e)
{
string username;
string role;
//get username
//get value from DDL



 UserManager.AddUserToRole(username,role);
}

之前我使用过GridViews,但还没有多少处理ListViews。我只是想要它,以便当我单击该特定行中的按钮时,我检索此信息,但列表视图中没有.Row ...任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用Button的CommandName属性

<asp:Button ID="AddRoleToUser"   CommandName="AddRole" 

背后的代码

protected void UserListView_ItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "AddRole"))
    {

      ListViewDataItem dataItem = (ListViewDataItem)e.Item;
      // this will gives you Row for your list items

      Label UserNameLabel=(Label)dataItem.FindControl("UserNameLabel");
    // you can find any control on row like this
      string YourNameLabel=UserNameLabel.Text;
    }
}