簡體   English   中英

用於DropDownList的GetUsersInRole數據源

[英]GetUsersInRole DataSource for DropDownList

嗨,我需要在DropDownList中顯示屬於特定ROLE的“用戶”列表。

這是我的代碼:

        // Find DropDownList
        DropDownList myUserList = (DropDownList)uxInsertAuthorInput.FindControl("uxUserListSelector");
        // Bind data to DropDownList
        myUserList.DataSource = Roles.GetUsersInRole("CMS-AUTHOR");
        myUserList.DataBind();
        myUserList.DataTextField = "UserName";

我的問題是訪問者在DropDownList中選擇一個項目。 傳遞的實際值是“UserName”而不是“UserId”。

訪問者從DropDownList選擇一個項目時,我需要使用“ UserId”值。

怎么做? 謝謝你的幫助

您可以采用所選的用戶名,然后調用Membership.GetUser 然后,您可以使用MembershipUser.ProviderUserKey屬性獲取用戶ID。

當然,如果目標是從用戶ID獲取MembershipUser,則在調用GetUser之后將擁有該用戶。

回到你的.aspx代碼(如果你使用了WYSIWYG則設計)。 更改SQL數據源中的查詢。 我通過使用類似的方式執行此操作:

        <asp:SqlDataSource ID="sqlUsersInRole0" runat="server" ConnectionString="<%$ ConnectionStrings:NCLWebConnectionString %>"
            SelectCommand="GetUsersInRole" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DefaultValue="MaintenanceWorkers" Name="rolename" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

我認為這也可以以編程方式完成,但為什么要這么麻煩?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM