Asp.Net - Gridview Rowcommand not triggered

Okay, let me explain first.

I have a gridview who displays some data and some additional columns which should fire a trigger.

The additional columns are: Rent a Movie (Huren)  and Buy a movie (Kopen).
But when i click on these linkbuttons, the only action that is happening is a reload of the page.

Code: 1 = The Gridview
Code 2 = The trigger

Note: They contain only page redirect for now, because of testing porpose.
<asp:GridView ID="gvFilm" runat="server" AllowPaging="True" PageIndex="15"
                            HorizontalAlign="Center" AutoGenerateColumns="False">
                            <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                            <EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                            <AlternatingRowStyle BackColor="#E4E4E4" />
                                <asp:BoundField datafield="DVDid" Visible="false" HeaderText="DVDId" 
                                    ReadOnly="true" showHeader="false" />
                                <asp:BoundField datafield="titel" HeaderText="Titel" ReadOnly="true" showHeader="false" />
                                <asp:BoundField datafield="genre" HeaderText="Genre" ReadOnly="true" showHeader="false" />
                                <asp:BoundField datafield="releasedatum" HeaderText="Releasedatum" ReadOnly="true" showHeader="false" />
                                <asp:BoundField datafield="huurprijs" HeaderText="Huurprijs" ReadOnly="true" showHeader="false" />
                                <asp:BoundField datafield="verkoopprijs" HeaderText="Verkoopprijs" ReadOnly="true" showHeader="false" />
                                <asp:ButtonField HeaderText="Huren" Text="Huren" 
                                    CommandName="HuurItem" />
                                <asp:ButtonField HeaderText="Kopen" Text="Kopen" CommandName="KoopItem"/>
'2 The trigger
    Private Sub gvFilm_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvFilm.RowCommand
            If e.CommandName = "HuurItem" Then
            End If
            If e.CommandName = "KoopItem" Then
            End If
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Who is Participating?
JPJ78Connect With a Mentor Commented:
It's hard to see from a screenshot.

- Did my example work for you?

- Try creating a standalone page (no masterpage) and see if you can get it to work. If it works, it would imply that there is something wrong with your masterpage.

- Try reducing your gridview to the simplest form. Only one button and see if it works. If it works, start adding columns one at a time to see whe the grid starts failing.
The code looks ok.

Do you Databind the GridView everytime the page loads or just when it's not a postback.
If you DataBind the GridView everytime the event is "ignored".

So make sure that you only DataBind when it's needed (first visit to the page, when the datasource has been modified)
I prefer to DataBind in PreRender.
it looks like e.CommandName is not evaluating to either of the strings you test for. Put in code to test what its value is.
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.

NicoJuicyAuthor Commented:
I databind it every time it loads.

tried wrapping the below load page code with if not ispostback, but doesn't work.
Then tried it in the event prerender, still doesn't work.

As the page keeps reloading.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (gvFilm.Rows.Count = True) Then
            gvFilm.UseAccessibleHeader = True
            gvFilm.HeaderRow.TableSection = TableRowSection.TableHeader
            gvFilm.FooterRow.TableSection = TableRowSection.TableFooter
        End If
    End Sub
    Sub filldgv()
        Dim fDOA As New filmDOA
        Dim dtfilm As New Data.DataTable
        dtfilm = fDOA.getFilms()
        gvFilm.DataSource = dtfilm
    End Sub

Open in new window

NicoJuicyAuthor Commented:
I attatched the above project file (cfilms).

It doesn't contain the database and the object classes.
It's just to find out where my mistake is.

It is compressed as a rar archive, because experts-exchange doesn't support the .vb extention.

Here's a simple example that works.

As I said before, your code looks ok.

My guess is that the controltree changes somehow.
Do you add controls dynamicly (from codebehind) on this page or in the masterpage?

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dt As New DataTable()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("1") = "1_1"
            dr("2") = "1_2"
            dr = dt.NewRow()
            dr("1") = "2_1"
            dr("2") = "2_2"
            GridView1.DataSource = dt
        End If
    End Sub
    Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim a As String = "a"
    End Sub
End Class
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Test._Default" %>
<!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">
        <asp:ButtonField HeaderText="Huren" Text="Huren" CommandName="HuurItem" />

Open in new window

NicoJuicyAuthor Commented:
I do not add any controls dynamicly.

The masterpage vb code only controls the session (for login etc)
and display's the sitemap menu: when they are logged in or logged out, a different menu is displayed.

The codebehind file is shown above in my first post.

I have added a screenshot, if you could see anything there (but i don't think so)

I still can't find my mistake. I have tried altering some code from your example, but it just won't work.

Could there be any other reason why the page just refreshes and won't execute the "button"/"command"?

NicoJuicyAuthor Commented:

I have figured it out. I had 1 line of code that said, object is not defined (in another page)
I didn't think that it had any influence on it, but probably it did.

I want to thank you JPJ78, it was impossible for you to know that.

But you earned the points anyway!

Thanks a lot!
NicoJuicyAuthor Commented:
Thanks, good help!
I'm glad you solved it!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.