javascript - 突出显示 Gridview LinkBut​​ton 上的行并单击确认警报 ASP.Net

标签 javascript c# jquery asp.net gridview

我有删除按钮并确认警报工作正常。但我想在显示警报之前对所选行的背景颜色进行着色。它工作正常,并且将“ View ”按钮的背景色显示为灰色,因为“ View ”按钮没有警报。但它不会显示“删除”按钮的背景颜色已更改,因为它首先发出警报,并且在"is"时,它会转到 RowCommand 事件。

如何更改删除消息之前提醒的背景颜色?

<asp:TemplateField HeaderText="Option">
    <ItemTemplate>
        <asp:LinkButton ID="btnEdit" CommandName="editRecord" Width="14px" Height="14px" aria-label="Left Align"
            CommandArgument='<%# Eval("DeptID") + "," + Eval("DeptName")%>' CssClass="" runat="server">
            <span class="glyphicon glyphicon-pencil" style="vertical-align:top;"></span>
        </asp:LinkButton>

        <asp:LinkButton ID="btnDelete" CommandName="deleteRecord" Width="14px" Height="14px" 
            OnClientClick="javascript:return confirm('Are you sure you want to Delete highlighted row?');"
            CommandArgument='<%# Eval("DeptID") + "," + Eval("DeptName")%>' CssClass="" runat="server">
                <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
        </asp:LinkButton>
    </ItemTemplate>
    <ItemStyle CssClass="col-md-1 col-sm-1 col-xs-1 grid-label-text-align grid-height-10"></ItemStyle>
</asp:TemplateField>

--代码隐藏

protected void grdDept_RowCommand(object sender, GridViewCommandEventArgs e)
{
    lblMessage.Visible = false;

    GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    int RowIndex = gvr.RowIndex;
    grdDept.Rows[RowIndex].BackColor = System.Drawing.Color.LightGray; 

    if (e.CommandName == "editRecord")
    {
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
        string deptID = commandArgs[0];
        string deptName = commandArgs[1];

        hfDeptID.Value = deptID;
        txtDeptName.Text = deptName;
        //btnAdd.Text = "Update";
        panel1.Visible = true;
        lblMessage.Visible = false;
    }
    else if (e.CommandName == "deleteRecord")
    {
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
        string deptID = commandArgs[0];

        if (ChildRecordExist(int.Parse(deptID)) == false)
        {
            DepartmentClass dept = new DepartmentClass();
            dept.DeptID = int.Parse(deptID);

            TransactionOptions options = new TransactionOptions();
            options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            options.Timeout = new TimeSpan(0, 15, 0);

            using (TransactionScope tran = new TransactionScope(TransactionScopeOption.Required, options))
            {
                if (deptID != "")
                {
                    if (dept.Delete() == true)
                    {
                        tran.Complete();
                        panel1.Visible = false;
                        lblMessage.Visible = false;
                    }
                }
            }
            LoadAllRecords("");
        }
    }
}

最佳答案

您必须为删除按钮设置一个类名称,例如class="delete"
现在添加这个 jquery:

$('.delete').click(function(){
    $('td').css('background-color', 'white') //change back all tds to their original background.
    $(this).closest('tr').css('background-color', 'red');
    return confirm('are you sure to delete?');
 });

添加此内容可以更好地查找所选行:

tr.selected{border-collapse: collapse;}

关于javascript - 突出显示 Gridview LinkBut​​ton 上的行并单击确认警报 ASP.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195282/

相关文章:

javascript - 什么时候应该使用 var 函数和 when 函数

javascript - 在 Chart.js 中使用 JSON 文件数据

c# - 有什么办法可以使扩展方法优先于泛型方法吗?

php - 无法将 jqgrid 表导出为 excel/pdf。

jquery - 单击 <div> 标记中的按钮时,按钮的 onClick 事件未执行

javascript - 删除两个字符串正则表达式之间的所有内容

php - 我如何检测客户端浏览器是否安装了 adobe reader 插件

javascript - react : open link in a new tab

c# - 如何使用 BinaryReader 正确读取 16 字节无符号整数

c# - Web 窗体项目中的小写 URL