Solved

ASP.NET GridView RowUpdating command doesn't fire

Posted on 2010-08-23
11
985 Views
Last Modified: 2012-05-10
Hello everyone.

I have a gridview with asp:commandfield for edit/update/cancel and delete commands. When I'm trying to update a row the RowUpdating event doesn't fire. The other 3 commands are working fine.
When I'm over the update link I see the following message on IE status bar: javascript:WebFor_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$body$gvWeighs$ctl06$ctl00","",true,"","",false,true))
In my web application I have the same markup code for the gridview in other pages and the RowUpdating event works fine . The only difference in this one is that in one of my edititemtemplate i have a dropdownlist.

I'm adding the markup code for that.

I'm databinding the data to the gridview manually with a dataset.

Can anyone help me??
Thanks in advnace.
<asp:GridView ID="gvWeighs" runat="server" CellPadding="4" ForeColor="#333333" BorderColor="CornflowerBlue" BorderStyle="Solid" Font-Names="Verdana" Font-Size="8.5pt" AutoGenerateColumns="False">
<Columns>
<asp:CommandField CancelText="Cancel" EditText="Edit" ShowEditButton="True" UpdateText="Save"/>
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/delete.jpg" ShowCancelButton="False" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="Car">
<EditItemTemplate>
<asp:DropDownList ID="Car" runat="server" DataSourceID="Cars" DataTextField="CarCode" DataValueField="CarID" SelectedValue='<%# Bind("CarID") %>'></asp:DropDownList>
<asp:SqlDataSource ID="Cars" runat="server" ConnectionString="<%$ ConnectionStrings:skip %>"
SelectCommand="SELECT [CarID], [CarCode] FROM [Cars]"</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Car" runat="server" Text='<%# Bind("CarCode") %>'></asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>

Open in new window

0
Comment
Question by:spiral2007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33499070
Kindly use the below given markup for your gridview.

You were missing the closing tag of asp:TemplateField and also the closing '>' for the SqlDataSource.

Hope it helps you solve your problem.
<asp:GridView ID="gvWeighs" runat="server" CellPadding="4" ForeColor="#333333" BorderColor="CornflowerBlue" BorderStyle="Solid" Font-Names="Verdana" Font-Size="8.5pt" AutoGenerateColumns="False">
<Columns>
<asp:CommandField CancelText="Cancel" EditText="Edit" ShowEditButton="True" UpdateText="Save"/>
<asp:CommandField ButtonType="Image" DeleteImageUrl="#" ShowCancelButton="False" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="Car">
<EditItemTemplate>
<asp:DropDownList ID="Car" runat="server" DataSourceID="Cars" DataTextField="CarCode" DataValueField="CarID" SelectedValue='<%# Bind("CarID") %>'></asp:DropDownList>
<asp:SqlDataSource ID="Cars" runat="server" ConnectionString="<%$ ConnectionStrings:skip %>"
SelectCommand="SELECT [CarID], [CarCode] FROM [Cars]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Car" runat="server" Text='<%# Bind("CarCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Open in new window

0
 

Author Comment

by:spiral2007
ID: 33499089
I have them in my markup. I probably deleted them when I copied the markup by mistake.
Thanks though
0
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33499324
Can u share your cs or vb file code.??
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:spiral2007
ID: 33499350
Which part do you want?
0
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33499624
The DataSource with which you are binding your gridview.
0
 

Author Comment

by:spiral2007
ID: 33499671
Here it is
Public Function GetWeighs(ByRef ds As DataSet, ByRef errorMessage As String) As Boolean
        Dim control As Boolean = False
        Dim da As SqlDataAdapter
        Dim sql As String = ""
        Try
            DatabaseConnection.OpenConnection()
            sql = "SELECT Weighs.CarID FROM Weighs INNER JOIN Cars ON Cars.CarID = Weighs.CarID ORDER BY Weighs.WeighDate"
            da = New SqlDataAdapter(sql, DatabaseConnection.connection)
            da.Fill(ds, "Weighs")
            control = True
        Catch ex As Exception
            control = False
            errorMessage = ex.Message
        Finally
            DatabaseConnection.CloseConnection()
        End Try
        Return control
End Function

Open in new window

0
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33499683
You have not  given the code for the GridView1.DataSource and DataBind() functions. I am asking that part where you are fetching records from DB and binding the gridview and then handling the events of the gridview.
0
 

Author Comment

by:spiral2007
ID: 33499738
Sorry copy mistake again..
This is the correct one

Public Sub loadWeighs()
        Dim ds As New DataSet
        Dim errorMessage As String = ""
        Dim control As Boolean = Me.GetWeighs(ds, errorMessage)
        If control Then
            Me.gvWeighs.DataSource = ds.Tables("Weighs")
            Me.gvWeighs.DataBind()
        Else
            If errorMessage = "" Then
                Me.gvWeighs.DataSource = ds.Tables("Weighs")
                Me.gvWeighs.DataBind()
                Me.lblerror.Text = ""
            Else
                Me.lblerror.Text = errorMessage
            End If
        End If
    End Sub

Protected Sub gvWeighs_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvWeighs.RowEditing
        If Session("update").ToString() = ViewState("update").ToString() Then
            Me.gvWeighs.EditIndex = e.NewEditIndex
            Me.lblerror.Text = ""
            Me.loadWeighs()
        End If
End Sub

Protected Sub gvWeighs_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvWeighs.RowUpdating
        If Session("update").ToString() = ViewState("update").ToString() Then
            Dim errorMessage As String = ""
            Dim control As Boolean = False
            Dim dCarID As DropDownList = Me.gvWeighs.Rows(e.RowIndex).FindControl("gvlblCarCode")
            Dim CarID As Integer = Convert.ToInt32(dCarID.SelectedValue)
            Dim WeighID As Integer = Convert.ToInt32(hfWeighID.Value)
            control = Me.UpdateWeighs(WeighID,CarID,errorMessage)
            If Not control Then
                Me.lblerror.Text = errorMessage
            Else
                Session("update") = Server.UrlEncode(System.DateTime.Now.ToString())
                Me.gvWeighs.EditIndex = -1
                Me.loadWeighs()
            End If
        End If
 End Sub

Public Function GetWeighs(ByRef ds As DataSet, ByRef errorMessage As String) As Boolean
        Dim control As Boolean = False
        Dim da As SqlDataAdapter
        Dim sql As String = ""
        Try
            DatabaseConnection.OpenConnection()
            sql = "SELECT Weighs.CarID FROM Weighs INNER JOIN Cars ON Cars.CarID = Weighs.CarID ORDER BY Weighs.WeighDate"
            da = New SqlDataAdapter(sql, DatabaseConnection.connection)
            da.Fill(ds, "Weighs")
            control = True
        Catch ex As Exception
            control = False
            errorMessage = ex.Message
        Finally
            DatabaseConnection.CloseConnection()
        End Try
        Return control
End Function

 Public Function UpdateWeighs(ByVal WeighID as Integer,ByVal CarID As Integer, ByRef errorMessage As String) As Boolean
        Dim control As Boolean = False
        Dim sql As String = ""
        Dim cmd As SqlCommand
        Try
            DatabaseConnection.OpenConnection()
            sql = "UPDATE Weighs SET CarID= {1} WHERE WeighID = {0} "
            sql = String.Format(sql, WeighID, CarID)
            cmd = New SqlCommand(sql, DatabaseConnection.connection)
            Dim rowsAffected As Integer = cmd.ExecuteNonQuery()
            If rowsAffected < 1 Then
                control = False
            Else
                control = True
            End If
        Catch ex As Exception
            control = False
            errorMessage = ex.Message
        Finally
            DatabaseConnection.CloseConnection()
        End Try
        Return control
End Function

Open in new window

0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33500288
add "OnRowUpdating" event in GridView Tag


<asp:GridView ID="gvWeighs" runat="server" CellPadding="4" ForeColor="#333333" BorderColor="CornflowerBlue" BorderStyle="Solid" Font-Names="Verdana" Font-Size="8.5pt" AutoGenerateColumns="False" OnRowUpdating="gvWeighs_RowUpdating">

Open in new window

0
 

Author Comment

by:spiral2007
ID: 33500335
Nothing changed..
0
 

Accepted Solution

by:
spiral2007 earned 0 total points
ID: 33521102
I solved it on my own.
I had to disable my control validators on a popup panel before editing.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

734 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