【问题标题】:Gridview field empties on RowUpdating commandRowUpdating 命令上的 Gridview 字段清空
【发布时间】:2016-04-07 22:04:28
【问题描述】:

我目前正在使用两个绑定到不同 sqldatasources 的 GridViews,它们都使用文本框接受每一行的值。当我去编辑然后更新行内的其他字段时,由文本框填充的值变成一个空值。在参数化值以及添加 RowCancelingEdit 方法等之前,它工作正常,但它已经停止。

是什么让“名称”值变为 Null?

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{            
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    String qry = "UPDATE [PlayerStatistics] SET [Name] = @Name, [Apps] = @Apps, [Minutes] = @Minutes, [Goals] = @Goals, [Assists] = @Assists, [Yellows] = @Yellows, [Reds] = @Reds WHERE [PlayerID] = @original_PlayerID";
    using (SqlCommand cmd = new SqlCommand(qry, con))
    {
        cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar);
        cmd.Parameters.AddWithValue("@Apps", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Minutes", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Goals", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Assists", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Yellows", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Reds", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@original_PlayerID", SqlDbType.Int);
        cmd.CommandType = CommandType.Text;
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

标记:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PlayerID" DataSourceID="SqlDataSource1" EmptyDataText="Please enter player(s) into the table." OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting1" Width="100%" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Apps" HeaderText="Apps" SortExpression="Apps" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Minutes" HeaderText="Minutes" SortExpression="Minutes" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Goals" HeaderText="Goals" SortExpression="Goals" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Assists" HeaderText="Assists" SortExpression="Assists" ControlStyle-Width="100%" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Yellows" HeaderText="Yellows" SortExpression="Yellows" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Reds" HeaderText="Reds" SortExpression="Reds" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:TemplateField HeaderText ="Edit/Delete">
            <ItemTemplate>
                <asp:LinkButton ID="BtnEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" />
                <span onclick="return confirm ('Are you Sure?')" >
                    <asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"/>
                </span>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="BtnUpdate" runat="server" CausesValidation="false" CommandName="Update" ConflictDetection="OverwriteChanges" Text="Update" />
                <asp:Button ID="BtnCancel" runat="server" CausesValidation="false" CommandName="Cancel" ConflictDetection="OverwriteChanges" Text="Cancel" />
            </EditItemTemplate>
            <ControlStyle Width="100%" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>


更新代码

使用存储过程:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    //String qry = "UPDATE [PlayerStatistics] SET [Name] = @Name, [Apps] = @Apps, [Minutes] = @Minutes, [Goals] = @Goals, [Assists] = @Assists, [Yellows] = @Yellows, [Reds] = @Reds WHERE [PlayerID] = @original_PlayerID";
    using (SqlCommand cmd = new SqlCommand("UpdatePlayer", con))
    {
        //cmd.Parameters.AddWithValue("@Name", GridView1.DataKeys[e.RowIndex].Values["Name"]);
        //cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar);
        cmd.Parameters.AddWithValue("@Apps", e.NewValues["Apps"]);
        cmd.Parameters.AddWithValue("@Minutes", e.NewValues["Minutes"]);
        cmd.Parameters.AddWithValue("@Goals", e.NewValues["Goals"]);
        cmd.Parameters.AddWithValue("@Assists", e.NewValues["Assists"]);
        cmd.Parameters.AddWithValue("@Yellows", e.NewValues["Yellows"]);
        cmd.Parameters.AddWithValue("@Reds", e.NewValues["Reds"]);
        cmd.Parameters.AddWithValue("@PlayerID", GridView1.DataKeys[e.RowIndex].Values["PlayerID"]);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.ExecuteNonQuery();
    }
    GridView1.EditIndex = -1;
    GridView1.DataBind();
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    AddWithValue 的第二个参数是值,而不是数据类型。由于 Name 字段在 GridView 中不可编辑,因此您可以将其从更新查询和参数中删除。您的事件处理程序如下所示:

    using (SqlCommand cmd = new SqlCommand("UpdatePlayer", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PlayerID", GridView1.DataKeys[e.RowIndex].Values["PlayerID"]);
        cmd.Parameters.AddWithValue("@Apps", e.NewValues["Apps"]);
        cmd.Parameters.AddWithValue("@Minutes", e.NewValues["Minutes"]);
        ...
    }
    

    您还应该将数据绑定到 GridView 并在 RowUpdating 事件处理程序的末尾重置 EditRowIndex

    GridView1.EditIndex = -1;
    BindData();
    

    【讨论】:

    • 我已经更改了代码以适应 e.NewValues 并返回此错误 "参数化查询 '(@Name nvarchar(4000),@Apps nvarchar(4000),@Minutes nvarchar(400' 需要参数 '@Name',但未提供该参数。" BindData 是指 GridView1.DataBind(),因为我使用的是 sqldatasource
    • 能否在问题中包含 GridView 的标记?
    • 我已将其添加到帖子中
    • 哪个字段包含名称?哪个包含应用程序?
    • 好吧,我想我终于修好了,不得不恢复到我最初发布的内容,并使“名称”变量不是只读的。你知道为什么会这样吗??非常感谢您的努力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2010-12-12
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多