Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am creating a website where I am using a gridview control.I want to secure my website.

So I want that only some specified users such as admin can change the gridview like inserting,updating and deleting.

These field will be invisible to all the others.

So how can I solve it?

Please help me.

Thanks in Advance.

Best Regards,

Golam kibria
Posted
Updated 9-Jun-11 20:29pm
v2

Hi there,

Not sure what you mean by "these field will be invisible to all others"

But if you are interested in controlling access to the grid-view:

I am assuming that you have some sort of user-info object that holds the current user's details (their access rights, ie the standard CRUD (Create, Read, Update, Delete))

To control whether a user can update a row, you might handle the RowValidating event from the DataGridView, check if the current user has permission to update the row, and if they don't, set the Cancel property of the event-arguments to True.

eg:

C#
void grid_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
    if (!CurrentUser.CanUpdate())
        e.Cancel = true;

}



There are lots of events on the data-grid-view, a lot of them can be cancelled -> that is how I would do it.


Cheers,

Simon.
 
Share this answer
 
Hope this[^] might help you.
 
Share this answer
 
User RowDataBound event of Gridview.

protected void gvr_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
       if ((e.Row.RowState & DataControlRowState.Edit) > 0)
       {
            Button btn = (Button)e.Row.Cells[6].FindControl("btnEdit");
            if(Session["UserType"].Equals("Admin"))//Checking whether the user is Admin or not
            {
              btn.Attributes.Add("style", "this.disabled=false;");
            }
            else
            {
              btn.Attributes.Add("style", "this.disabled=true;");
            }
       }
   }
}


Also you can use display attribute to hide/show the buttons.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900