簡體   English   中英

ASP.NET-可以創建一個DetailsView,僅具有特定角色的用戶將某些字段視為可編輯的嗎?

[英]ASP.NET - Can you make a DetailsView where only users with specific roles see some fields as editable?

例如,假設我只希望管理員能夠在詳細信息視圖中查看和編輯CustomerID,主持人可以看到此值,但該值不可編輯,而普通用戶甚至看不到它。 這可能嗎?

干得好。 我嘗試了許多Google搜索,但沒人能清楚地解釋它。 您必須在PreRender事件上執行此操作。 請不要在此代碼段中使用.net中的成員資格提供程序來檢查用戶是否在角色中。 如果您有自己的自定義表,則必須編寫一個自定義函數來檢查用戶是否在您的自定義角色中。 也請不要因為此解決方案使用的是ItemTemplates而不是BoundFields。

protected void detailsView_OnPreRender(object sender, EventArgs e)
    {
        if (dvPackage.CurrentMode == DetailsViewMode.Edit)
        {
           //disables/enables a the dropdown for Process Status if the user has the RLIST role
           TextBox txtCustomID = (TextBox)Utilities.FindControlRecursive(dvPackage, "txtCustomID ");
           txtCustomID.Visible = false;
           if (User.IsInRole("Admin"))
           {
              txtCustomID.Visible = true;

         }
     }

這是查找控件的遞歸函數。 免費的。

public static Control FindControlRecursive(Control ctlRoot, string sControlId)
    {
        // if this control is the one we are looking for, break from the recursion    
        // and return the control.    
        if (ctlRoot.ID == sControlId)
        {
            return ctlRoot;
        }
        // loop the child controls of this parent control and call recursively.    
        foreach (Control ctl in ctlRoot.Controls)
        {
            Control ctlFound = FindControlRecursive(ctl, sControlId);
            // if we found the control, return it.        
            if (ctlFound != null)
            {
                return ctlFound;
            }
        }// we never found the control so just return null.    
        return null;
    }

我建議Web用戶控件或多視圖根據用戶角色來處理切換視圖。

暫無
暫無

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

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