Hi Dave,
I believe you have a few options:
1. Keep the buttons in the templates (such as EditItemTemplate), and access them through code via the FindControl method of the FormView:
Code:
if (FormView1.CurrentMode = FormViewMode.Edit) // make sure the update template is loaded
{
Control foundControl = FormView1.FindControl("UpdateButton");
if (foundControl != null && !User.IsInRole("My Role"))
foundControl.Visible = false;
}
2. Leave the buttons outside as you mentioned, but instead of using CommandName do form operations in the button click methods:
Code:
protected void MyUpdateButton_OnClick(object sender, EventArgs e)
{
FormView1.UpdateItem(false); // false = no validation
}
protected void Page_Load(object sender, EventArgs e)
{
if (!User.IsInRole("My Role"))
MyUpdateButton.Visible = false;
}
3. If possible, simply don't show edit or insert templates, but instead show only the read-only template if they do not have permissions, and do not include any buttons on the read-only template which would allow the user to go into edit or insert mode:
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (User.IsInRole("My Editor Role"))
FormView1.ChangeMode(FormViewMode.Edit);
else if (User.IsInRole("My Creator Role"))
FormView1.ChangeMode(FormViewMode.Insert);
// Default FormView mode is read-only
}
Of course you could always use a combination of these 3, and at a last resort you can always override the FormView's ItemUpdating or ItemDeleting methods for any last-minute checking, but obviously preventing the actions from occuring in the first place would be the best option.
Hope this helps,
Roger
Bookmarks