2012-09-26 30 views
0

我正在嘗試利用詳細信息視圖併爲幾個級別的用戶授權提供編輯功能。基本上,1級用戶無法更新預定義的一組字段,但2級用戶可以更新這些字段。我試圖在定義EditTemplate時將字段設置爲visible=false,然後在DataBind中測試授權,如果用戶有權更新字段(請參見下面的代碼示例),則將其設置爲visible=true。像魅力一樣工作,除了我注意到當1級用戶更新允許更新的字段時,數據庫中的visible=false字段將被設置爲null(覆蓋)。所以,一直在嘗試各種選項,不要有重複的意見等詳細信息視圖和字段編輯授權

代碼片段: ASPX ....

<asp:TemplateField HeaderText="<%$ Resources:Resource, Level %>" 
    SortExpression="LevelId"> 
     <EditItemTemplate> 
      <asp:DropDownList ID="LevelList" runat="server" 
       DataTextField="LevelDesc" DataValueField="LevelId"> 
      </asp:DropDownList> 
     </EditItemTemplate> 
     <HeaderStyle HorizontalAlign="Right" /> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="<%$ Resources:Resource, Level1 %>" 
    SortExpression="Level1Date" Visible="false" > 
     <EditItemTemplate> 
      <asp:TextBox ID="Level1" runat="server" Text='<%# 
       Bind("Level1Date", "{0:d}") %>' /> 
      <asp:CompareValidator ID="CompareValidator1" runat="server" 
       ErrorMessage="Please enter a valid date (m/d/y)" 
       ControlToValidate="Level1" Operator="DataTypeCheck" 
       Type="Date" Display="Dynamic"> 
      </asp:CompareValidator> 
     </EditItemTemplate> 
     <HeaderStyle HorizontalAlign="Right" /> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="<%$ Resources:Resource, Level2 %>" 
     SortExpression="Level2Date" Visible="false" > 
     <EditItemTemplate> 
      <asp:TextBox ID="Level2" runat="server" Text='<%# 
       Bind("Level2Date", "{0:d}") %>' /> 
      <asp:CompareValidator ID="CompareValidator2" runat="server" 
       ErrorMessage="Please enter a valid date (m/d/y)" 
       ControlToValidate="Level2" Operator="DataTypeCheck" 
       Type="Date" Display="Dynamic"> 
      </asp:CompareValidator> 
     </EditItemTemplate> 
     <HeaderStyle HorizontalAlign="Right" /> 
    </asp:TemplateField> 

    <asp:TemplateField HeaderText="<%$ Resources:Resource, Level4 %>" 
     SortExpression="Level4Date" Visible="false" > 
     <EditItemTemplate> 
      <asp:TextBox ID="Level4" runat="server" Text='<%# 
       Bind("Level4Date", "{0:d}") %>' /> 
</Fields> 

aspx.cs摘要:SNIPPET

<name>_DataBound(object sender, EventArgs e) 
     { 
. 
. 
. 
if (User.IsInRole("yyy") || User.IsInRole("xxx)) 
{ 
    OfficialProfileInfo.Fields[2].Visible = true; 
    OfficialProfileInfo.Fields[3].Visible = true; 
} 

回答

0

必須設置DataKeyNames屬性用於自動更新,刪除和插入要運行的DetailsView控件的功能。

在更新時如果某些字段不應該更改,可以將它們放入密鑰中。

if (User.IsInRole("yyy") || User.IsInRole("xxx)) 
{ 
    OfficialProfileInfo.Fields[2].Visible = true; 
    OfficialProfileInfo.Fields[3].Visible = true; 
} 
else 
{ 
    OfficialProfileInfo.DataKeyNames = "Level4Date" 
} 

注:的DataKeyNames是一個逗號分隔的字段名稱/

+0

感謝您的答覆的列表。當我使用嘗試他們時,我得到一個錯誤,指出「不能隱式轉換類型'字符串'字符串[]'」 – PGB