Click here to Skip to main content
14,421,109 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I am having doubts regarding my project.

I need to give an enable option for delete button to only authorized users.
For others it should be in disabled mode.

Please, can any one help me.

I am posting my code.
Urgent for me.

thanks in advance!

protected void Page_Load(object sender, EventArgs e)
   {

       if (!IsPostBack)
       {

           FillGridView();

       }
   }

   private void FillGridView()
   {
                  DataSet ds = null;
           ConferenceBL confBL = new ConferenceBL();
           ds = confBL.GetConferenceRoom();
           ViewState["ds"] = ds;

           if (ds != null)
           {
               gvconference.Visible = true;
               gvconference.DataSource = ds;
               gvconference.DataBind();


           }
       }
          }

   protected void gvconference_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       try
      {
        gvconference.PageIndex = e.NewPageIndex;
        FillGridView();
      }

      catch (Exception ex)
      {
          if (log.IsErrorEnabled)
          {
              log.Error(ex.Message);
          }
      }
   }


   protected void gvconference_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string EmployeeId = Session["EmployeeId"].ToString();
                Button b = (Button)e.Row.FindControl("btnDelete");
              b.Attributes.Add("onclick", "javascript:return " +
             "confirm('Are you sure you want to delete this record " +
      DataBinder.Eval(e.Row.DataItem, "SequenceId") + "')");
                }
                            }

           }

       catch (Exception ex)
       {
           if (log.IsErrorEnabled)
           {
               log.Error(ex.Message);
           }
       }

   }

   protected void gvconference_RowCommand(object sender, GridViewCommandEventArgs e)
   {
                  if (e.CommandName == "Delete")
           {
               string EmployeeId = Session["EmployeeId"].ToString();
               int SequenceId = Convert.ToInt32(e.CommandArgument);

               // Delete the record
               DeleteRecordById(SequenceId,EmployeeId);




           }
       }
             }

   protected void gvconference_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       try
       {

           int SequenceId = (int)gvconference.DataKeys[e.RowIndex].Value;
           string EmployeeId = Session["EmployeeId"].ToString();

           DeleteRecordById(SequenceId,EmployeeId);
       }
       catch (Exception ex)
       {
           if (log.IsErrorEnabled)
           {
               log.Error(ex.Message);
           }
       }
   }

   private void DeleteRecordById(int SequenceId,string EmployeeId)
   {
                 ConferenceBL confBL = new ConferenceBL();
           confBL.DeleteConferenceRoom(SequenceId,EmployeeId);
           FillGridView();
       


         }
Posted
Updated 25-Mar-11 5:21am
v3
Comments
Pavel Yermalovich 25-Mar-11 7:57am
   
A ton of code to answer about enabling/disabling just one button.
mandarapu 25-Mar-11 8:44am
   
enable and disable only but not working.i checked with all the conditions.
Dalek Dave 25-Mar-11 10:21am
   
Edited for Grammar, Syntax and Readability.
Rate this:
Please Sign up or sign in to vote.

Solution 1

protected void gvconference_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string EmployeeId = Session["EmployeeId"].ToString();
                Button b = (Button)e.Row.FindControl("btnDelete");
              b.Attributes.Add("onclick", "javascript:return " +
             "confirm('Are you sure you want to delete this record " +
      DataBinder.Eval(e.Row.DataItem, "SequenceId") + "')");
                }
//***********************************
if(CheckYourConditions)// Check for the condition here
{
 b.Enabled = true;
}
else
{
 b.Enabled = false;
}

//***********************************
                            }

           }

       catch (Exception ex)
       {
           if (log.IsErrorEnabled)
           {
               log.Error(ex.Message);
           }
       }

   }
   
Comments
mandarapu 25-Mar-11 7:56am
   
i checked all the conditions but not working.
Prerak Patel 25-Mar-11 7:58am
   
and where do you do that? I didn't see it in your code.
mandarapu 25-Mar-11 8:07am
   
i gave condition as
if(session["loginId"]==EmployeeId)
{
b.enabled=true;
}
else
b.enabled=false;
when i am keeping this condition and checking all the buttons are in disable mode.
mandarapu 25-Mar-11 9:08am
   
hello plz suggest me.
Dalek Dave 25-Mar-11 9:57am
   
5 for the hard work.
Rate this:
Please Sign up or sign in to vote.

Solution 2

in Page Load check User is authorized or not if he is authorized then

enabled Your delete button other wise make it disabled
   
Comments
Dalek Dave 25-Mar-11 9:57am
   
Good Call
Rate this:
Please Sign up or sign in to vote.

Solution 3

All depends how you have implemented an authorization.
Write in your Page_Load something like this:

//...
btnDelete.Enabled=IsAuthorized;
//...


And implement IsAuthorized property depending your authorization. This property will be useful to configure the rest of UI.
   
v2
Comments
Dalek Dave 25-Mar-11 9:57am
   
Good Answer.
Rate this:
Please Sign up or sign in to vote.

Solution 4

you can try something like this

for a grid view which is
<ItemTemplate>
         <asp:Button   ID="btnDel" Text ="Delete"  ImageUrl ="images/green_down_15_10.bmp" CommandArgument='<%# Eval("ItemID")%>'  CommandName ="Delete"

           ToolTip="Delete" Enabled ="True"  runat="server" >
            </asp:Button   >
       </ItemTemplate>



in the code behind you have the set of user in the datatable who can have the delete rights and loop through it:

boolean deleteenable = true;
if(dtUserhavingrights.select("User = logged on user").length >0)
{
deleteenable = true;
}
else
{
deleteenable = false;
}

for each GridviewRow gvr in mygridview.rows
{
Button btn;
btn = (Btton)gvr.findcontrol("btnDel")
btn.enabled =deleteenable;
}


Please feel free to ask if you have any questions.
   
v2
Comments
mandarapu 25-Mar-11 8:10am
   
can u tell me where i need to change the above code i posted.in my code i was not using the datatable.plz check my code.
Dalek Dave 25-Mar-11 9:58am
   
Earns a 5.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100