RowUpdating 事件不起作用

RowUpdating event not working

我有一个网格视图,我需要用 RowUpdating 事件更新它,但是更新后新值没有出现,数据库更新为旧值。 这是我的代码

 protected void gvContactInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvContactInfo.EditIndex = e.NewEditIndex;
        bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
    }
    protected void gvContactInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lbl = ((Label)gvContactInfo.Rows[e.RowIndex].FindControl("lblContactidno"));
        DropDownList ddl = ((DropDownList)gvContactInfo.Rows[e.RowIndex].FindControl("ddlInfoType"));
        TextBox txtinfo = ((TextBox)gvContactInfo.Rows[e.RowIndex].FindControl("txtValueE"));
        TextBox txtext = ((TextBox)gvContactInfo.Rows[e.RowIndex].FindControl("txtExt"));

        string queryContactInfo = "update tblContactInfo set ContactInfoType='"+ddl.SelectedItem.Text+"',ContactInfo='"+txtinfo.Text+"',Ext='"+txtext.Text+"' where ContactID=" + int.Parse(lbl.Text.Trim()) + "";
        Connection = new SqlConnection(ConnString);
        Connection.Open();
        SqlCommand cmd = new SqlCommand(queryContactInfo, Connection);
        cmd.ExecuteNonQuery();
        Connection.Close();
        gvContactInfo.EditIndex = -1;
        bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
    }
    protected void gvContactInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvContactInfo.EditIndex = -1;
        bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
    }

我的数据绑定代码如下:

public void bindingGVContacts(int contactID)
    {
        int contactID1 = contactID;
        string queryContactInfo = "SELECT * FROM tblContactInfo where ContactID=" +  contactID1 + "";
        Connection = new SqlConnection(ConnString);
        Connection.Open();
        ds = new DataSet();
        DataTable dt = new DataTable();
        ad = new SqlDataAdapter(queryContactInfo, ConnString);
        ad.Fill(ds, "queryContactInfo");
        ad.Fill(dt);
        Connection.Close();
        if (ds.Tables["queryContactInfo"].Rows.Count > 0)
        {
            gvContactInfo.Columns[0].Visible = true;
            gvContactInfo.DataSource = ds.Tables["queryContactInfo"];
            gvContactInfo.DataBind();
            gvContactInfo.Columns[0].Visible = false;

            foreach (GridViewRow grow in gvContactInfo.Rows)
            {
                Label lbl = ((Label)grow.FindControl("lblContactidno"));
                DropDownList ddl = ((DropDownList)grow.FindControl("ddlInfoType"));
                DataRow[] dr = dt.Select("ContactNoID=" + lbl.Text.Trim() + "");
                if (dr.Length != 0)
                {
                    ddl.SelectedItem.Selected = false;
                    if (ddl.Items.FindByText(dr[0]["ContactInfoType"].ToString()) != null)
                        ddl.Items.FindByText(dr[0]["ContactInfoType"].ToString()).Selected = true;
                }
            }
        }
        else
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            gvContactInfo.DataSource = dt;
            gvContactInfo.DataBind();
            gvContactInfo.Rows[0].Visible = false;
        }
    }

这是我的 gridview 的 aspx 代码:

<asp:GridView runat="server" ID="gvContactInfo" ShowHeader="true" ShowHeaderWhenEmpty="true" Enableviewstate="true"
                    AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="gvContactInfo_RowEditing" OnRowUpdating="gvContactInfo_RowUpdating" OnRowCancelingEdit="gvContactInfo_RowCancelingEdit" OnRowCommand="gvContactInfo_RowCommand"
                    CssClass=" CategoriesTable table table-striped table-bordered CategoriesTable1" 
                    onrowdatabound="gvContactInfo_RowDataBound">
                         <Columns>
                         <asp:TemplateField HeaderText="Value" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:Label ID="lblContactidno" runat="server" Text='<%#Eval("ContactNoID")%>' Font-Bold="true"></asp:Label>
                             </ItemTemplate>
                         </asp:TemplateField>

                             <asp:TemplateField HeaderText="INFO Type" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:DropDownList ID="ddlInfoType" runat="server">
                                <asp:ListItem Value="Address" Text="Address"></asp:ListItem>
                                <asp:ListItem Value="Email-Personal" Text="Email-Personal"></asp:ListItem>
                                <asp:ListItem Value="Email-Work" Text="Email-Work"></asp:ListItem>
                                <asp:ListItem Value="Phone-Home" Text="Phone-Home"></asp:ListItem>
                                <asp:ListItem Value="Phone-Work" Text="Phone-Work"></asp:ListItem>
                                <asp:ListItem Value="Phone-Mobile" Text="Phone-Mobile"></asp:ListItem>
                                </asp:DropDownList>
                             </ItemTemplate>
                                 <FooterTemplate>
                                     <asp:DropDownList ID="ddlInfoType" runat="server">
                                        <asp:ListItem Value="Address" Text="Address"></asp:ListItem>
                                        <asp:ListItem Value="Email-Personal" Text="Email-Personal"></asp:ListItem>
                                        <asp:ListItem Value="Email-Work" Text="Email-Work"></asp:ListItem>
                                        <asp:ListItem Value="Phone-Home" Text="Phone-Home"></asp:ListItem>
                                        <asp:ListItem Value="Phone-Work" Text="Phone-Work"></asp:ListItem>
                                        <asp:ListItem Value="Phone-Mobile" Text="Phone-Mobile"></asp:ListItem>
                                     </asp:DropDownList>
                                 </FooterTemplate>
                             </asp:TemplateField>

                             <asp:TemplateField HeaderText="Value" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:TextBox ID="txtValue" runat="server" Text='<%#Eval("ContactInfo")%>'></asp:TextBox>
                             </ItemTemplate>
                                 <FooterTemplate>
                                     <asp:TextBox ID="txtValue" runat="server" Text=""></asp:TextBox>
                                 </FooterTemplate>
                                 <EditItemTemplate>
                                    <asp:TextBox ID="txtValueE" runat="server" Text='<%#Eval("ContactInfo")%>'></asp:TextBox>
                                 </EditItemTemplate>
                             </asp:TemplateField>

                             <asp:TemplateField HeaderText="Extension" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:TextBox ID="txtExtension" runat="server" Text='<%#Eval("Ext")%>'></asp:TextBox>
                             </ItemTemplate>
                                 <FooterTemplate>
                                     <asp:TextBox ID="txtExtension1" runat="server" Text=""></asp:TextBox>
                                 </FooterTemplate>
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtExt" runat="server" Text='<%#Eval("Ext")%>'></asp:TextBox>
                                 </EditItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Action" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:LinkButton runat="server" CommandName="Edit" CausesValidation="false">Edit</asp:LinkButton>
                             </ItemTemplate>
                                 <EditItemTemplate>
                                     <asp:LinkButton runat="server" CommandName="Update" CausesValidation="false">Update</asp:LinkButton>
                                     <asp:LinkButton runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                                 </EditItemTemplate>
                                 <FooterTemplate>
                                     <asp:Button runat="server" Text="ADD" CommandName="Insert"></asp:Button>
                                 </FooterTemplate>
                             </asp:TemplateField>
                        </Columns>
                </asp:GridView>

请提供解决方案。

设置调试器并检查是否将正确的(新)值传递给 sql 查询字符串,并在更新时使用 try-catch 块捕获任何异常。

此外,使用字符串构建 sql 查询也不是一个好主意。你应该使用 Parameter 来防止 SQL-Injection.

这个 example 显示了 SQL 注射会发生什么。

而这个 example 展示了如何使用 Parameter

在 C# 中防止 SQL 注入

只需检查您是否正在使用断点 gvContactInfo_RowUpdating 更改事件中获取新值

而不是直接使用 FindControl 选项读取旧值,

Label lbl = ((Label)gvContactInfo.Rows[e.RowIndex].FindControl("lblContactidno"));

您应该使用 GridViewUpdateEventArgs.NewValues Property 属性 来获取所有新值作为 key/value 对。

string lblStr = e.NewValues[0].ToString(); //lblContactidno

编辑

您正在使用 Row_Updating 事件中的 FindControl 方法读取控件的现有值。问题是您的新值尚未更新,并且正在更新中。因此,它正在拉出旧的价值观。旧值和新值都作为 key/value 对存储在事件 GridViewUpdateEventArgs 中。因此,您必须从那里获取新值 [NewValues 属性]。我在这里建议的代码是只读取标签的值。检查您是否正在为它获取新值,因为它基于标签是网格中的第一个控件的假设。

我有另一种更新方式,即借助 RowCommand 方法

 protected void gvContactInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
if (e.CommandName.Equals("Update"))
        {
            GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int RowIndex = gvr.RowIndex;
            Label lbl = ((Label)gvContactInfo.Rows[RowIndex].FindControl("lblContactidno"));
            DropDownList ddl = ((DropDownList)gvContactInfo.Rows[RowIndex].FindControl("ddlInfoType"));
            TextBox txtinfo = ((TextBox)gvContactInfo.Rows[RowIndex].FindControl("txtValueE"));
            TextBox txtext = ((TextBox)gvContactInfo.Rows[RowIndex].FindControl("txtExt"));

            string queryContactInfo = "update tblContactInfo set ContactInfoType='" + ddl.SelectedItem.Text + "',ContactInfo='" + txtinfo.Text + "',Ext='" + txtext.Text + "' where ContactNoID=" + int.Parse(lbl.Text.Trim()) + "";
            Connection = new SqlConnection(ConnString);
            Connection.Open();
            SqlCommand cmd = new SqlCommand(queryContactInfo, Connection);
            cmd.ExecuteNonQuery();
            Connection.Close();
            gvContactInfo.EditIndex = -1;
        }
}