Jake
Jake

Reputation: 3350

Conditionally Allow Editing in FormView?

What would be the best way to go about programatically enabling or disabling the ability to edit a FormView in C#? Ideally I'm looking for something like:

if (user.IsAdmin())
    FormView.Editable = true;

else
    FormView.Editable = false;

But I can't seem to find a property that would let me do that. I have an Edit Item template defined, and so the "Edit" button always comes up when I see the FormView.

If you need any other information, please let me know. I intentionally didn't post any code to keep this as general as possible - I can tailor it to my own situation. Thank you!

Upvotes: 2

Views: 1780

Answers (1)

Brad Christie
Brad Christie

Reputation: 101614

Best advice I can give is as follows:

Setup your itemtemplate with the edit button you're looking to target. e.g.

<asp:Formview ...>
  ...
  <ItemTemplate>
    ...
    <asp:Button runat="server" id="edit_button" CommandName="Edit" Text="Edit" />
    ...
  </ItemTemplate>
</asp:FormView>

Then, within your code-behind you can allow/disallow that button based on permissions. e.g.

void Page_Load(...)
{
  ...
  ((Button)this.formView.FindControl("edit_button")).Enabled = User.IsAdmin();
  ...
}

Upvotes: 2

Related Questions