简体   繁体   English

单击时如何禁用GridView中的链接按钮

[英]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. 我有两个LinkButton's所谓批准,在我拒绝GridView ,我想我批准LinkButtonDisabled点击时,就可以了。 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. 您需要使用禁用的控制重新绑定网格,但是还需要检查itembound事件中的状态并禁用。 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 好吧,您还没有显示aspx链接按钮,所以我假设您的链接按钮是这个

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

Now you should add a javascript function like this on the page:: 现在,您应该在页面上添加如下所示的javascript函数::

<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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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