简体   繁体   中英

How to disable a link button in gridview when clicked

I have two LinkButton's called Approve and Reject in my GridView and I want my Approve LinkButton to be Disabled when click on it. I tried the following code but it's not working.

protected void gvManagerTimeSheet_RowCommand(object sender, GridViewCommandEventArgs e)   
{
        if (e.CommandName == "ApproveRow")
        {
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            LinkButton lnkbtn = (LinkButton)row.FindControl("lbApprove");
            lnkbtn.Enabled = false;

            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            int TimeSheetId = Convert.ToInt32(e.CommandArgument);

            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spApproveTimeSheet ", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TimeSheetId", TimeSheetId);

                con.Open();
                cmd.ExecuteNonQuery();

                GetManagerTimeSheets();
            }
         }

}

You need to Rebind the grid with disabled control, but also you need to check the status in itembound event and disable. For that you can use session or hidden field.

protected void rg_OnItemCommand(object source, GridCommandEventArgs e)
{
   // your logic 
   hdFlag.value = "val" // id of the data item to hide if more than one use array
  // rebind logic for gird
}

protected void rg_ItemDataBound(object sender, GridItemEventArgs e)
{
  if(hdFlag.value == "id")
  {
    // Find the control and hide 
  }
}

Well you haven't shown your aspx link button so i assuming that your link button is this

<asp:LinkButton id="linkBtn" runat="server" text="Approve"  OnClientClick="Disable(this);"/>

Now you should add a javascript function like this on the page::

<script>
function Disable(link)
{
link.disabled = result;
}
</script>

Now when you click on the page your button will get disabled.

尝试这个

 LinkButton lbApprove = (LinkButton)e.Row.Cells[0].Controls[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM