Rodrigo Souza
Rodrigo Souza

Reputation: 7332

How to get the new values while editing a gridview?

I'm trying to update some records in my table using the GridView Item templates. It's like this:

<asp:GridView ID="grdCursos" runat="server" CssClass="grdTable" 
                AutoGenerateColumns="False" DataKeyNames="CourseID">
                <Columns>
                    <asp:TemplateField HeaderText="ID" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblID" runat="server" Text='<%#Bind("[CourseID]") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Nome">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtCourseName" runat="server" Text='<%#Bind("[CourseName]") %>' Width="50"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCourseName" runat="server" Text='<%#Bind("[CourseName]") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="# de Questões">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMaxQuestions" runat="server" Text='<%#Bind("[CourseMaxQuestions]") %>' Width="20"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblMaxQuestions" runat="server" Text='<%#Bind("[CourseMaxQuestions]") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Data de atribuição">
                        <ItemTemplate>
                            <asp:Label ID="lblCourseSince" runat="server" 
                                Text='<%#Bind("[CourseSince]","{0:d}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Nome do arquivo">
                        <EditItemTemplate>
                            <asp:FileUpload ID="fileCourse" runat="server"/>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCourse" runat="server" Text='<%#Bind("[CourseFileName]") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:CommandField ButtonType="Image" EditImageUrl="~/site/edit.jpg" 
                        ShowEditButton="True" DeleteImageUrl="~/site/cancel.jpg" 
                        UpdateImageUrl="~/site/accept.jpg" CancelImageUrl="~/site/cancel.jpg" 
                        ShowDeleteButton="True" CancelText="Cancelar" DeleteText="Inativar" 
                        EditText="Editar" UpdateText="Atualizar"></asp:CommandField>
                    <asp:CommandField ShowEditButton="True" />
                </Columns>

As soon as I click in Edit Link, some of my GriView's columns become a TextBox which I can edit. Then, when I click update button it calls my grdCursos_RowUpdating method. The problem is that I can't get the new values the user gave as input. I call a MsgBox to show the values I want to update and it shows the old values only.

This is my method:

Protected Sub grdCursos_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdCursos.RowUpdating
    Dim updatingRow As GridViewRow = grdCursos.Rows(e.RowIndex) 'Recupera a linha que está sendo editada

    'Recupera os dados cadastrados
    Dim ID As Label = updatingRow.FindControl("lblID")
    Dim txtCourseName As TextBox = updatingRow.FindControl("txtCourseName")
    Dim txtMaxQuestions As TextBox = updatingRow.FindControl("txtMaxQuestions")
    Dim CourseFile As FileUpload = updatingRow.FindControl("fileCourse")

    MsgBox(String.Format("ID={0}, Nome do curso={1}, # de questoes={2}, Nome do arquivo={3}", ID.Text, txtCourseName.Text, txtMaxQuestions.Text, CourseFile.FileName))
    Exit Sub
    ....

How can I get the new values?

Upvotes: 1

Views: 5304

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460018

The short answer: e.NewValues

From MSDN:

Gets a dictionary containing the revised values of the non-key field name/value pairs in the row to update.

Use the NewValues property (dictionary) to access the values of the revised non-key fields in the row to update.

The NewValues property is automatically populated with the name/value pairs of the revised non-key fields in the row. A separate entry is added to the NewValues property for each non-key field in the row.

To determine the field name of an entry, use the DictionaryEntry.Key property of a System.Collections.DictionaryEntry object contained in the NewValues dictionary. To determine the value of an entry, use the DictionaryEntry.Value property.

Note

The primary key field or fields are not included in this dictionary. To access the values of the primary key field or fields, use the Keys property. To access the original values of the non-key fields in the row, use the OldValues property.

Example:

Sub CustomersGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

    ' Use the CopyTo method to copy the DictionaryEntry objects in the 
    ' NewValues collection to an array.
    Dim records(e.NewValues.Count - 1) As DictionaryEntry
    e.NewValues.CopyTo(records, 0)

    ' Iterate through the array and HTML encode all user-provided values 
    ' before updating the data source.
    Dim entry As DictionaryEntry
    For Each entry In records

      e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString())

    Next
End Sub

Upvotes: 1

Related Questions