简体   繁体   English

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

[英]Highlight Row on Gridview LinkButton Clicked with Confirmation Alert ASP.Net

I have Delete button and confirm alert is working fine.我有删除按钮并确认警报工作正常。 But I want to color the BackColor of selected row prior to show alert.但我想在显示警报之前为所选行的 BackColor 着色。 Its working fine and showing BackColor to Grey for View button because there is not alert for View button.它工作正常,并且对于查看按钮显示 BackColor 为灰色,因为没有查看按钮的警报。 But it does not show BackColor changed for Delete button as it alert first and on Yes, it goes to RowCommand event.但它没有显示删除按钮的 BackColor 更改,因为它首先发出警报,并且在是时,它转到 RowCommand 事件。

How to change backcolor prior alerting for delete message?如何在删除消息之前更改背景色?

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

--Code Behind --背后的代码

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

You must set a class name for delete buttons for example class="delete"您必须为删除按钮设置一个类名,例如class="delete"
Now add this jquery:现在添加这个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?');
 });

and adding this make better look for selected row:并添加它可以更好地查找所选行:

tr.selected{border-collapse: collapse;}

使用onmousedown事件突出显示要删除的行,然后使用onclick事件进行实际删除。

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

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