Asp.net gridview, get the value of the associated field in the code behind when in edit mode

In asp.net (C #) I use gridview and edit mode so the user can easily change values. But I need to check the value that the user enters in OnRowUpdating, when using this code I always get the old value, and not the new value that I entered. How can I get a 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.. } 
+4
source share
5 answers

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

You have added both id and value to the DataKeyNames GridView property, for example. DataKeyNames="id,value" . This is similar to indicating that they are both primary keys. If you want the value to be edited, you must remove it from DataKeyNames.

+4
source

Strange, I use autogenerate = false, but it does not work, 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 this, but it would be nice to know how to use "NewValues" instead ...

+2
source

Try using the OnRowUpdated event. OnRowUpdating is created before the grid updates the row. OnRowUpdated occurs after the grid updates the row.

+1
source

I just did a quick test with GridView and SqlDataSource, and e.NewValues["value"] gave me a new value, as expected. What you do is correct as you posted the code.

The only thing I can think of is that you did not set AutoGenerateColumns="false" in your GridView when using BoundFields. If you donโ€™t install it, you will get two sets of columns - BoundFields and generated ones. This will cause a collision as you will be sending two columns with the same name.

If this is not a problem, you will need to send more code.

+1
source
 <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> 
-2
source

All Articles