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

时间:2022-08-24 10:42:46

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.

我在GridView中有两个名为Approve and Reject的LinkBut​​ton,我希望在点击它时我的Approve LinkBut​​ton被禁用。我尝试了以下代码,但它不起作用。

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();
            }
         }

}

3 个解决方案

#1


0  

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.

现在当您点击页面时,您的按钮将被禁用。

#2


0  

try this

尝试这个

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

#3


0  

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.

您需要使用禁用控件重新绑定网格,但您还需要检查itembound事件中的状态并禁用。为此,您可以使用会话或隐藏字段。

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 
  }
}

#1


0  

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.

现在当您点击页面时,您的按钮将被禁用。

#2


0  

try this

尝试这个

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

#3


0  

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.

您需要使用禁用控件重新绑定网格,但您还需要检查itembound事件中的状态并禁用。为此,您可以使用会话或隐藏字段。

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 
  }
}