formview and allowwrite problem

formview and allowwrite problem

Old forum URL: forums.lhotka.net/forums/t/7707.aspx


robertoenbarcelona posted on Wednesday, September 30, 2009

Hi,
i have just a simple question about allowwrite in a formview.
i have a BO, Customer, a person with Admin role can edit all fields, a person with Reader role only cannot edit sensible data, cannot for example edit Rank field.
i put the allowwrite and allowred rules for them accord to the examples.
then i put a formview in an Asp.Net page..
i disable the fields that cannot be edit if a Reader is loged-on.. so they cannot edit, when databind appens the page raise an authorization exception making the e.mapvalue!
this sound confusing to me, first of all  the field is not changed, so there is no reason to set the same value for this rank field, second this cause the authorization exception when the user didn't try to write this value!
where is the error? how is the best pratics for this situations in Asp.Net?
thanks 

ozhug replied on Thursday, October 08, 2009

Not sure about best practice;

I place controls for both cases on the form and then use the FormView_prerender and the AuthorizationRules to set up which controls on the form are visible to the current user.

protected void MyFormView_PreRender(object sender, EventArgs e)
{
//two texboxes on the form one for users allowed to edit and one for others.
TextBox tb = (TextBox)fv.FindControl("TextBoxNoEdit");
TextBox tbAllowEdit = (TextBox)fv.FindControl("TextBoxAllowEdit");
if (Csla.Security.AuthorizationRules.CanEditObject(typeof(myBO)))
{
tbAllowEdit.Visible = true;
tbNoEdit.Visible = false;
}

}

on the page the TextBoxAllowEdit has visible set to false.

Copyright (c) Marimer LLC