views:

9445

answers:

5

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..
}
+1  A: 

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.

Fernando
But I need to check this before my database is updated and as I'm using a SqlDataSource the Update of the database will be done before OnRowUpdated. Is this impossible?
A: 

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.

Dan Diplo
A: 

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...

+1  A: 

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.

Dan Diplo
Aha, that's why, thanks! I have always used the dataKeyNames to be able to reach the values from my gridview in codebehind. So if I need to access "value" somewhere I thought I could just add it to datakeynames. But that was wrong then.. Is there another way to reach the values from the datasource or gridview directly? I want a way to just write something like DataSource["value"] to get the value. Is there?
A: 
<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>
Raghunath