C# Gridview未更新值

C# Gridview未更新值,c#,asp.net,visual-studio,gridview,C#,Asp.net,Visual Studio,Gridview,我试图让gridview更新SQL记录,但它没有更新值 以下是aspx代码: <asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false" onrowcancelingedit="GridView1_RowCancelling" OnRowDeleting ="GridView1_RowDeleting" onrowediting="GridVi

我试图让gridview更新SQL记录,但它没有更新值

以下是aspx代码:

<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false" 
    onrowcancelingedit="GridView1_RowCancelling" 
    OnRowDeleting ="GridView1_RowDeleting" 
    onrowediting="GridView1_RowEditing"
    onrowupdating="GridView1_RowUpdating" 
    DataKeyNames="RID"
    CssClass="mGrid" 
    PagerStyle-CssClass="pgr"  
    AlternatingRowStyle-CssClass="alt">  


      <Columns>           

         <asp:TemplateField Visible="false" HeaderText="RID">
            <ItemTemplate>
              <asp:Label runat="server" ID="RID" Text='<%#Bind("RID")%>' />
            </ItemTemplate>
         </asp:TemplateField>

         <asp:TemplateField HeaderText="Short Description">
            <ItemTemplate>
              <asp:Label runat="server" ID="short_lbl" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox runat="server" ID="SHORT_DESCRIPTION" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
            <asp:RequiredFieldValidator runat="server" ID="valShort" ControlToValidate="SHORT_DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
            </EditItemTemplate>
         </asp:TemplateField>


              <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
              <asp:Label runat="server" ID="desc_label" Text='<%#Bind("DESCRIPTION") %>' />
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox runat="server" ID="DESCRIPTION" Text='<%#Bind("DESCRIPTION") %>' />
            <asp:RequiredFieldValidator runat="server" ID="valLast" ControlToValidate="DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
            </EditItemTemplate>
         </asp:TemplateField>



      <asp:TemplateField HeaderText="Action">
      <ItemTemplate>
      <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
      <br />
      <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
      </ItemTemplate>
      <EditItemTemplate>
      <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
      <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
      </EditItemTemplate>
      </asp:TemplateField>

   </Columns>

</asp:GridView>

如果我放置一个断点并查看该值,我可以看到它不是新键入的值。我不确定发生了什么。

尝试将Eval更改为Bind。Eval是只读的,因此无法实际更新该值。

尝试将Eval更改为绑定。Eval是只读的,因此您无法实际更新该值。

使用此代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="id" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                SortExpression="id" />
            <asp:BoundField DataField="regGroupID" HeaderText="regGroupID" 
                SortExpression="regGroupID" />
            <asp:BoundField DataField="amountReceived" HeaderText="amountReceived" 
                SortExpression="amountReceived" />
            <asp:BoundField DataField="other" HeaderText="other" SortExpression="other" />
        </Columns>
    </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Table]" 
    UpdateCommand="UPDATE [Table] SET [regGroupID] = @regGroupID, [amountReceived] = @amountReceived, [other] = @other WHERE [id] = @id">
    <DeleteParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>

    <UpdateParameters>
        <asp:Parameter Name="regGroupID" Type="Int32" />
        <asp:Parameter Name="amountReceived" Type="Decimal" />
        <asp:Parameter Name="other" Type="String" />
        <asp:Parameter Name="id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

这里我将id设置为主键

表结构是

使用此代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="id" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                SortExpression="id" />
            <asp:BoundField DataField="regGroupID" HeaderText="regGroupID" 
                SortExpression="regGroupID" />
            <asp:BoundField DataField="amountReceived" HeaderText="amountReceived" 
                SortExpression="amountReceived" />
            <asp:BoundField DataField="other" HeaderText="other" SortExpression="other" />
        </Columns>
    </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Table]" 
    UpdateCommand="UPDATE [Table] SET [regGroupID] = @regGroupID, [amountReceived] = @amountReceived, [other] = @other WHERE [id] = @id">
    <DeleteParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>

    <UpdateParameters>
        <asp:Parameter Name="regGroupID" Type="Int32" />
        <asp:Parameter Name="amountReceived" Type="Decimal" />
        <asp:Parameter Name="other" Type="String" />
        <asp:Parameter Name="id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

这里我将id设置为主键

表结构是


我认为问题在于您在
页面加载事件上绑定了
网格

如果
(!IsPostBack)

例子
我认为问题在于您在
页面加载
事件上绑定了
网格

如果
(!IsPostBack)

例子
仍然没有更新不幸的是,我更新了原始代码。仍然没有更新不幸的是,我更新了原始代码。你在
SHORT\u DESCRIPTION.Text,DESCRIPTION.Text和RID.Text中得到了什么?
我得到了来自数据库的原始值(不是我键入并单击“提交”获取的更新值)你在
SHORT\u DESCRIPTION.Text、DESCRIPTION.Text和RID.Text中得到了什么?我从数据库中得到了原始值(不是我键入并单击“提交”获得的更新值),哇,好主意!你是对的,它是在页面加载上做的。你的猜测应该得到一枚奖牌!哇,好球!你是对的,它是在页面加载上做的。你的猜测应该得到一枚奖牌!
private void Page_Load()
{
    if (!IsPostBack)
    {
        //bind your grid here.
    }
}