简体   繁体   中英

Asp.net gridview, get boundfield value in code behind when in edit mode

In asp.net (c#) I'm using a gridview and edit mode to make it easy for the user to change values. But I need to check the value that the user enter in OnRowUpdating, when using this code, I always get the old value not the new value I entered. How can I get the new value?

<asp:GridView ID="MyGridView" OnRowUpdating="MyGridViewUpdate" DataKeyNames="id,value">
    <Columns>
        <asp:BoundField ReadOnly="false" DataField="value" DataFormatString="{0:0.##}" />
        <asp:CommandField ShowEditButton="true" EditImageUrl="~/images/iconedit.png" ButtonType="Image" CancelImageUrl="~/images/iconclose.png" UpdateImageUrl="~/images/iconedit.png" />
   </Columns>
</asp:GridView>

In codebehind:

protected void MyGridViewUpdate(object sender, GridViewUpdateEventArgs e)
{
    string test = e.NewValues["value"].ToString();
    //Test always give the old value, I need the new value the user added..
}

Ahh, looking more carefully I think I see the problem:

You have added both id and value to the DataKeyNames property of the GridView eg. DataKeyNames="id,value" . This is like specifying they are both primary keys. If you want value to be editable then you should remove it from the DataKeyNames.

Strange, I'm using autogenerate=false but it isn't working, this is my full code:

<asp:SqlDataSource ID="DSValues" 
    runat="server"
    DataSourceMode="DataSet"  
    ConnectionString="..."
    ProviderName="MySql.Data.MySqlClient"
    SelectCommand="CALL spValues();" 
    UpdateCommand="UPDATE table SET value=?value WHERE id=?id;">
</asp:SqlDataSource>

<asp:GridView ID="MyGridView" OnRowUpdating="MyGridViewUpdate" DataKeyNames="id,value" DataSourceID="DSValues" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField ReadOnly="false" DataField="value" DataFormatString="{0:0.##}" />
    <asp:CommandField ShowEditButton="true" EditImageUrl="~/images/iconedit.png" ButtonType="Image" CancelImageUrl="~/images/iconclose.png" UpdateImageUrl="~/images/iconedit.png" />
</Columns>
</asp:GridView>

//SpValues() is a stored procedure that just do: "SELECT id,value FROM table"

This worked:

(MyGridView.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text 

I could just use that, but it would be fine to know how to use "NewValues" instead...

Try using the OnRowUpdated event. The OnRowUpdating is raised before the grid updates the row. The OnRowUpdated is raised after the grid updates the row.

I've just done a quick test with a GridView and SqlDataSource and e.NewValues["value"] gave me the new value, as expected. What you are doing is correct, so far as the code you posted.

The only thing I can think of is that you have not set AutoGenerateColumns="false" on your GridView when you are using BoundFields. If you don't set it you will get two sets of columns - the BoundFields and the generated ones. This will cause a clash, as you will be sending two columns with the same name.

If that isn't the problem, the you will need to post more code.

<asp:GridView id="gvDiagnostics" runat="server" 
         AllowPaging="True" AutoGenerateColumns="False"
         BackColor="White" BorderColor="#DEDFDE" 
         BorderStyle="None" BorderWidth="1px"
         CellPadding="4" CssClass="label" ForeColor="Black"
         GridLines="Vertical" Width="100%"  
         DataKeyNames="SNo">
  <footerstyle backcolor="#CCCC99" />
  <columns>
    <asp:BoundField DataField="SNo" HeaderText="SNo">
      <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
      <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle"></HeaderStyle>
    </asp:BoundField>
    <%--<asp:BoundField DataField="ProductId" SortExpression="ProductId" HeaderText="S.No"></asp:BoundField>--%>
    <asp:TemplateField SortExpression="Code" HeaderText="Code">
      <EditItemTemplate>
        <asp:TextBox runat="server" Text='<%# Bind("Code") %>' id="TextBox1"></asp:TextBox>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:LinkButton id="lbtnCode" runat="server" Text='<%# Eval("Code") %>'   OnClick="lbtnSNo_Click"></asp:LinkButton>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="ShortList" SortExpression="ShortList" HeaderText="ShortList"></asp:BoundField>
    <asp:BoundField DataField="Description" SortExpression="Description" HeaderText="Description"></asp:BoundField>
  </columns>
  <rowstyle backcolor="#F7F7DE" />
  <emptydatatemplate>
    <asp:LinkButton id="lbtnProductId" runat="server" Text='<%# Eval("ProductCategory") %>'></asp:LinkButton>
  </emptydatatemplate>
  <selectedrowstyle backcolor="#C04000" font-bold="True" forecolor="White" />
  <pagerstyle backcolor="#F7F7DE" forecolor="Black" horizontalalign="Right" />
  <headerstyle backcolor="#6B696B" font-bold="True" forecolor="White" />
  <alternatingrowstyle backcolor="White" />
</asp:GridView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM