网格视图中的 RowUpdating 事件未触发

RowUpdating event in grid view is not firing

我有一个网格视图,我想更新行中的数据。我写了一个 C# 代码,我注意到 RowUpdating 事件没有触发。 我在我的网格中使用模板字段。 RowCommand 事件工作正常,但是,RowUpdating 事件没有触发。 我应该怎么办?我需要你的帮助。

活动如下:

protected void grdPeople_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    try
    {
        SqlConnection con = new SqlConnection(strCon);
        if (con.State == ConnectionState.Closed)
            con.Open();
        SqlCommand com = new SqlCommand("InsertUpdatePerson", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.AddWithValue("@pr_id", Convert.ToInt32(grdPeople.DataKeys[e.RowIndex].Value.ToString()));
        com.Parameters.AddWithValue("@pr_name", (grdPeople.FooterRow.FindControl("txtNameFooter") as TextBox).Text.Trim());
        com.Parameters.AddWithValue("@pr_gender", (grdPeople.FooterRow.FindControl("rdoMaleFooter") as RadioButton).Checked);
        com.Parameters.AddWithValue("@pr_nationality", (grdPeople.FooterRow.FindControl("txtNationalityFooter") as TextBox).Text.Trim());
        com.Parameters.AddWithValue("@pr_idn", (grdPeople.FooterRow.FindControl("txtIDnoFooter") as TextBox).Text.Trim());
        com.Parameters.AddWithValue("@pr_passport", (grdPeople.FooterRow.FindControl("txtPassportFooter") as TextBox).Text.Trim());
        com.Parameters.AddWithValue("@pr_resident", (grdPeople.FooterRow.FindControl("rdoYesFooter") as RadioButton).Checked);
        com.Parameters.AddWithValue("@pr_phone", (grdPeople.FooterRow.FindControl("txtPhoneFooter") as TextBox).Text.Trim());
        com.Parameters.AddWithValue("@pr_email", (grdPeople.FooterRow.FindControl("txtEmailFooter") as TextBox).Text.Trim());
        com.Parameters.Add(new SqlParameter("@ERR_MESSAGE", SqlDbType.NVarChar, 255)).Direction = ParameterDirection.Output;
        com.Parameters.Add(new SqlParameter("@ERR_NUMBER", SqlDbType.Int)).Direction = ParameterDirection.Output;

        com.ExecuteNonQuery();

        int ErrNo = int.Parse(com.Parameters["@ERR_NUMBER"].Value.ToString());
        if (ErrNo != 0)
        {
            lblSuccess.Text = "Data saved successfully.";
            grdPeople.EditIndex = -1;
        }
        else
        {
            lblError.Text = com.Parameters["@ERR_MESSAGE"].Value.ToString();
        }
    }
    catch (Exception ex)
    {
        lblSuccess.Text = "";
        lblError.Text = ex.Message;
    }
}

下面是asp页面的设计:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ValidationGridView.Default" EnableEventValidation="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Table runat="server">
                    <asp:TableRow>
                        <asp:TableCell>
                            <asp:TextBox runat="server" ID="txtSearch" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button runat="server" ID="btnSearch" Text="Search" />
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
            </div>
            <div>
                <asp:GridView ID="grdPeople" runat="server" CellPadding="4" ForeColor="#333333" 
                    GridLines="None" AutoGenerateColumns="false" 
                    OnRowCommand="grdPeople_RowCommand"
                    OnRowEditing="grdPeople_RowEditing"
                    OnRowCancelingEdit="grdPeople_RowCancelingEdit"
                    OnRowUpdating="grdPeople_RowUpdating"
                    ShowFooter="true" DataKeyNames="pr_id">

                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

                    <Columns>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label runat="server" Text='<%# Bind("pr_name") %>' ID="lblName" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtName" Text='<%# Bind("pr_name") %>' runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="Name is required." ForeColor="Red" Text="*" ValidationGroup="Fields" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox runat="server" ID="txtNameFooter" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtNameFooter" ErrorMessage="Name is required." ForeColor="Red" Text="*" ValidationGroup="FieldsFooter" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Gender">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_gender") %>' runat="server" ID="lblGender" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:RadioButton ID="rdoMale" Text ="Male" Checked='<%# Bind("pr_gender_value") %>' GroupName="Gender" runat="server" />
                                <asp:RadioButton ID="rdoFemale" Text="Female" runat="server" GroupName="Gender" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:RadioButton ID="rdoMaleFooter" Text="Male" checked="true" runat="server" GroupName="Gender" />
                                <asp:RadioButton ID="rdoFemaleFooter" Text="Female" runat="server" GroupName="Gender" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Nationality">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_nationality") %>' runat="server" ID="lblNationality" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtNationality" Text='<%# Bind("pr_nationality") %>' runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="txtNationality" runat="server" ErrorMessage="Nationality is required." ForeColor="Red" Text="*" ValidationGroup="Fields" />
                                </FooterTemplate>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtNationalityFooter" runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtNationalityFooter" ErrorMessage="Nationality is required." ForeColor="Red" ValidationGroup="FieldsFooter" Text="*" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="ID Number">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_idn") %>' runat="server" ID="lblIDNo" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtIDNo" Text='<%# Bind("pr_idn") %>' runat="server" />
                                <asp:CustomValidator ID="cstIDNo" runat="server" Text="*" ControlToValidate="txtIDNo" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="Fields" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtIDnoFooter" runat="server" />
                                <asp:CustomValidator ID="cstIDNoFooter" runat="server" Text="*" ControlToValidate="txtIDnoFooter" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="FieldsFooter" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Passport">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_passport") %>' runat="server" ID="lblPassport" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtPassport" Text='<%# Bind("pr_passport") %>' runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtPassport" ErrorMessage="Passport is required." ForeColor="Red" ValidationGroup="Fields" Text="*" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtPassportFooter" runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtPassportFooter" ErrorMessage="Passport is required." ForeColor="Red" ValidationGroup="FieldsFooter" Text="*" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Resident">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_resident") %>' runat="server" ID="lblResident" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:RadioButton ID="rdoYes" Text="Yes" Checked='<%# Bind("pr_resident_value") %>' GroupName="Resident" runat="server" />
                                <asp:RadioButton ID="rdoNo" Text="No" runat="server" GroupName="Resident" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:RadioButton ID="rdoYesFooter" Text="Yes" Checked="true" GroupName="Resident" runat="server" />
                                <asp:RadioButton ID="rdoNoFooter" Text="No" runat="server" GroupName="Resident" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Phone">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_phone") %>' runat="server" ID="lblPhone" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtPhone" Text='<%# Bind("pr_phone") %>' runat="server" />
                                <asp:CustomValidator ID="cstPhone" runat="server" Text="*" ControlToValidate="txtPhone" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="Fields" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtPhoneFooter" runat="server" />
                                <asp:CustomValidator ID="cstPhoneFooter" runat="server" Text="*" ControlToValidate="txtPhoneFooter" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="FieldsFooter" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Email">
                            <ItemTemplate>
                                <asp:Label Text='<%# Bind("pr_email") %>' runat="server" ID="lblEmail" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEmail" Text='<%# Bind("pr_email") %>' runat="server" />
                                <asp:CustomValidator ID="cstEmail" runat="server" Text="*" ControlToValidate="txtEmail" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="Fields" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtEmailFooter" runat="server" />
                                <asp:CustomValidator ID="cstEmailFooter" runat="server" Text="*" ControlToValidate="txtEmailFooter" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="FieldsFooter" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton runat="server" ImageUrl="~/icons/edit.png" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px" />
                                <asp:ImageButton runat="server" ImageUrl="~/icons/delete.png" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" />
                                <asp:ImageButton runat="server" ImageUrl="~/icons/email.png" CommandName="Mail" ToolTip="Mail" Width="20px" Height="20px" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:ImageButton runat="server" ImageUrl="~/icons/save.png" CommandName="Save" ToolTip="Save" Width="20px" Height="20px" ValidationGroup="Fields"/>
                                <asp:ImageButton runat="server" ImageUrl="~/icons/cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:ImageButton runat="server" ID="btnInsert" ImageUrl="~/icons/add.png" ValidationGroup="FieldsFooter" CommandName="Insert" ToolTip="Insert" Width="20px" Height="20px" />
                            </FooterTemplate>
                        </asp:TemplateField>

                    </Columns>
                </asp:GridView>
                <br />
                <asp:Label runat="server" ID="lblSuccess" ForeColor="Green" />
                <br />
                <asp:Label runat="server" ID="lblError" ForeColor="Red" />
            </div>
            <div>
                <asp:ImageButton ID="btnImportExl" runat="server" ImageUrl="~/icons/excel.png" Width="40px" Height="40px" />
            </div>
            <div>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" ValidationGroup="Fields" />
            </div>
        </form>
    </body>
</html>

我添加上面的网格设计是为了向您展示我是如何设计它以及如何设置它的属性的。 谢谢。

在您的 GridViewEditItemTemplate 中,您遗漏了带有 CommandName="Update" 的按钮,该按钮将触发 grdPeople_RowUpdating 事件。

<EditItemTemplate>
    <asp:ImageButton runat="server" ImageUrl="~/icons/save.png" CommandName="Save" ToolTip="Save" Width="20px" Height="20px" ValidationGroup="Fields"/>
    <asp:ImageButton runat="server" ImageUrl="~/icons/cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
</EditItemTemplate>

解决方案

将保存按钮 CommandName 的值从 CommandName="Save" 更改为 CommandName="Update"

<EditItemTemplate>
    <asp:ImageButton runat="server" ImageUrl="~/icons/save.png" CommandName="Update" ToolTip="Save" Width="20px" Height="20px" ValidationGroup="Fields"/>
    <asp:ImageButton runat="server" ImageUrl="~/icons/cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
</EditItemTemplate>

参考资料

Edit and Update Record in GridView in ASP.Net