Posted on 2009-04-18
Last Modified: 2012-05-06
I want to do an update on GRIDVIEW IN AJAX Application with VB.Net.
When i click on the UPDATE button on GRIDVIEW, how can i select the new values and old values. Can someone give me the piece of code in at the event of ROWUPDATING.

Question by:salmanfazal
    1 Comment
    LVL 18

    Accepted Solution


    Please review the attached code sample which does just what You're after (managing the edit / update events from code behind). To make it AJAX, just put the GridView inside and ajax update panel.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="GridViewPage.aspx.vb" Inherits="GridViewPage" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" 
                CellPadding="4" ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#EFF3FB" />
                    <asp:CommandField CausesValidation="False" ShowEditButton="True" />
                    <asp:BoundField DataField="ID" HeaderText="ID:" SortExpression="ID" 
                        Visible="True" />
                    <asp:TemplateField HeaderText="First Name:" SortExpression="FirstName">
                            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                    <asp:TemplateField HeaderText="Last Name:" SortExpression="LastName">
                            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    <asp:TemplateField HeaderText="Company:" SortExpression="Company">
                            <asp:TextBox ID="txtCompany" runat="server" Text='<%# Bind("Company") %>'></asp:TextBox>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Company") %>'></asp:Label>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
    Imports System.Data
    Imports System.Data.SqlClient
    Partial Class GridViewPage
        Inherits System.Web.UI.Page
        Private dbConn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
            End If
        End Sub
        Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
            GridView1.EditIndex = e.NewEditIndex
            GridView1.Rows(e.NewEditIndex).Cells(1).Enabled = False 'Protect ID Column
        End Sub
        Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
            GridView1.EditIndex = -1
        End Sub
        Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
            Dim strUpdateCustomers As String = "UPDATE Customers SET FirstName = @FirstName, LastName = @LastName, Company = @Company WHERE (ID = @ID)"
            Dim cmdUpdateCustomers As New SqlCommand(strUpdateCustomers, dbConn)
            cmdUpdateCustomers.Parameters.AddWithValue("@ID", GridView1.DataKeys(e.RowIndex).Values(0))
            cmdUpdateCustomers.Parameters.AddWithValue("@FirstName", CType(GridView1.Rows(e.RowIndex).FindControl("txtFirstName"), TextBox).Text)
            cmdUpdateCustomers.Parameters.AddWithValue("@LastName", CType(GridView1.Rows(e.RowIndex).FindControl("txtLastName"), TextBox).Text)
            cmdUpdateCustomers.Parameters.AddWithValue("@Company", CType(GridView1.Rows(e.RowIndex).FindControl("txtCompany"), TextBox).Text)
                If Not dbConn.State = ConnectionState.Open Then
                End If
            Catch ex As Exception
                'Logic to handle exception
            End Try
            If Not dbConn.State = ConnectionState.Closed Then
            End If
            e.Cancel = True
            GridView1.EditIndex = -1
        End Sub
        Private Sub BindGrid(ByVal gv As GridView)
            Dim strSelectCustomers As String = "SELECT * FROM Customers ORDER BY LastName"
            Dim cmdSelectCustomers As New SqlDataAdapter(strSelectCustomers, dbConn)
            Dim dtCustomers As New DataTable()
            gv.DataSource = dtCustomers.DefaultView()
        End Sub
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now