对Grid视图中不需要的控件的行事件弹出限制

时间:2017-02-24 06:56:37

标签: c# jquery asp.net c#-4.0

我已经制作了一个弹出框,可以在点击时显示Row事件,并在行中显示详细信息,但我不想在显示Gridview内部的插入/编辑/删除按钮时显示该框。为此,我使用了jquery。

这是我的代码

$(function () {
        var grid = document.getElementById('<%= gvDetails.ClientID%>');
        $('tr', grid).on('click', function (obj, b) {
            if (['Update', 'Edit', 'Add'].indexOf(obj.target.type) == "image")
                return true;
            else
            $("#id").html($(".clsempid", $(this).closest("tr")).html());
            $("#name").html($(".clsempid1", $(this).closest("tr")).html());
            $("#department").html($(".clsempid2", $(this).closest("tr")).html());
            $("#age").html($(".clsempid3", $(this).closest("tr")).html());
            $("#sal").html($(".clsempid4", $(this).closest("tr")).html());
            $("#dialog").dialog({
                title: "View Details",
                buttons: {
                    Ok: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
        });
    });


<asp:TemplateField HeaderText="Edit/Delete" HeaderStyle-Width="5%">
                    <EditItemTemplate>
                        <asp:ImageButton ID="imgbtnUpdate"  CommandName="Update" runat="server" ImageUrl="~/Images/update.png" ToolTip="Update" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/cancel.png" ToolTip="Cancel" Height="20px" Width="20px" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Insert.png" ToolTip="Edit" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.png" ToolTip="Delete" Height="10px" Width="10px" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/Add.png" CommandName="Add" Width="20px" Height="20px" ToolTip="Add new User" ValidationGroup="validaiton" OnClientClick="GetGridFooterRowvalues()" />
                    </FooterTemplate>
                </asp:TemplateField>`<asp:GridView ID="gvDetails" style="background-color:lightgreen;     border-collapse: collapse;
background-color: lightgreen;
margin-top: -53px;" DataKeyNames="EmpId,EmpName" runat="server"

            AutoGenerateColumns="false" HeaderStyle-BackColor="#61A6F8"
            ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="white"
            CellPadding="10" CellSpacing="0"
            AllowPaging="true" PageSize="5"
            OnRowCommand="gvDetails_RowCommand"  OnRowUpdating="gvDetails_RowUpdating" 
            OnRowEditing="gvDetails_RowEditing" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
            OnRowDeleting="gvDetails_RowDeleting" OnPageIndexChanging="OnPaging"
            OnSelectedIndexChanged="OnSelectedIndexChanged" AllowSorting="True" OnRowDataBound="gvDetails_OnRowDataBound">


            <Columns >


                <asp:TemplateField  HeaderText="EmployeeID">
                    <ItemTemplate>
                        <asp:Label ID="lblempid" runat="server" class="clsempid"
                            Text='<%# Eval("EmpId")%>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtempid" onkeypress=" return numeric(event,this);"
                            MaxLength="5" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvempid" runat="server" ControlToValidate="txtempid" Text="*" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>



                <asp:TemplateField HeaderText="EmployeeName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtempname" runat="server"   Text='<%#Eval("EmpName") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblempname" runat="server" class="clsempid1" Text='<%#Eval("EmpName") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtempname1" runat="server"  AutoPostBack="false" onkeypress=" return onlyAlphabets(event,this);" />
                        <asp:RequiredFieldValidator ID="rfvempname" runat="server" ControlToValidate="txtempname1" Text="*" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="Department">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtdep" runat="server" Text='<%#Eval("Dep") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbldep" runat="server" class="clsempid2" Text='<%#Eval("Dep") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtdep1" runat="server" />
                        <asp:RequiredFieldValidator ID="rfvdep" runat="server" ControlToValidate="txtdep1" Text="*" ValidationGroup="validaiton" />
                    </FooterTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="Age">
                    <HeaderTemplate>
                        Age 
                         <br></br>



                    </HeaderTemplate>

                    <EditItemTemplate>
                        <asp:TextBox ID="txtage" runat="server" Text='<%#Eval("Age") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblage" runat="server" class="clsempid3" Text='<%#Eval("Age") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtage1" runat="server" MaxLength="3" onkeypress=" return numeric(event,this);" ErrorMessage="*" ControlToValidate="txtage1" onchange="handleChange(this);" />
                        <asp:RequiredFieldValidator ID="rfvage" runat="server" ControlToValidate="txtage1" ErrorMessage="*" ValidationGroup="validaiton" />

                        </EditItemTemplate>
                    </FooterTemplate>
                </asp:TemplateField>`

1 个答案:

答案 0 :(得分:0)

您是否尝试过将代码放在{}。

之间
    var grid = document.getElementById('<%= gvDetails.ClientID%>');
    $('tr', grid).on('click', function (obj, b) {
        if (['Update', 'Edit', 'Add'].indexOf(obj.target.type) == "image"){
            return true;
        }else{
            $("#id").html($(".clsempid", $(this).closest("tr")).html());
            $("#name").html($(".clsempid1", $(this).closest("tr")).html());
            $("#department").html($(".clsempid2", $(this).closest("tr")).html());
            $("#age").html($(".clsempid3", $(this).closest("tr")).html());
            $("#sal").html($(".clsempid4", $(this).closest("tr")).html());
            $("#dialog").dialog({
                title: "View Details",
                buttons: {
                    Ok: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
        }
    });
});