GridView CommandArgument suddenly stopped working

Discussion in 'ASP .Net' started by Leon Mayne, Apr 22, 2008.

  1. Leon Mayne

    Leon Mayne Guest

    Hello,
    I have a gridview that's bound to a generic list of business objects and am
    using the RowCommand event to capture user clicks on actions. This was
    working fine up until today, but now e.CommandArgument being passed into the
    RowCommand event handler is always coming back as an empty string and
    causing an invalid cast exception.

    The GridView itself has a hyperlink field to display the object name, and
    several TemplateFields to allow the user to perform actions on the record
    (such as move up, move down, delete etc.). Here's one of the template
    fields:

    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
    <asp:ImageButton ID="imgDelete1" runat="server" CausesValidation="false"
    CommandName="deletesection" CommandArgument='<%#Eval("PolicySectionId")%>'
    ImageUrl="~/Images/delete.gif" AlternateText="Delete" OnClientClick="return
    confirm('Are you sure you want to delete the selected section?');" />
    </ItemTemplate>
    </asp:TemplateField>

    You can see that I am manually setting the CommandArgument to the
    PolicySectionId property of the business object when it binds. Then in my
    code behind file I have:

    Private Sub GridView_RowCommand(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    GridView1.RowCommand
    ' Check what action we are performing
    Select Case e.CommandName
    Case "deletesection"
    ' Delete (archive) the section
    Dim objSection As New SafetyPolicySection(CInt(e.CommandArgument))
    objSection.Archive(CType(Session("CurrentUser"), User).UserId)
    ' etc
    End Select
    Me.BindGridView()
    End Sub

    This used to work OK, but now e.CommandArgument is an empty string each time
    I try. I checked the history of the files and there doesn't seem to be any
    changes that would affect the functionality. Does anyone know what might be
    wrong?
    Leon Mayne, Apr 22, 2008
    #1
    1. Advertising

  2. Leon Mayne

    Leon Mayne Guest

    "Leon Mayne" <> wrote in message
    news:...
    > Hello,
    > I have a gridview that's bound to a generic list of business objects and
    > am using the RowCommand event to capture user clicks on actions. This was
    > working fine up until today, but now e.CommandArgument being passed into
    > the RowCommand event handler is always coming back as an empty string and
    > causing an invalid cast exception.


    OK, I've found that a change made to add subheadings for the gridview is
    messing up some of the records, for example:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound
    ' Make sure this isn't the header
    If e.Row.DataItem IsNot Nothing Then
    Dim intCurrentId As Integer = CType(e.Row.DataItem, MyObject).Id
    If intCurrentId Mod 10 = 0 Then
    ' Add a row before this record
    Dim tblGrid As Table = CType(Me.GridView1.Controls(0),
    Table)
    ' Get the rowindex of the current row
    Dim intIndex As Integer = tblGrid.Rows.GetRowIndex(e.Row)
    ' Create a new row to hold our subheading
    Dim gvrSubHeading As New GridViewRow(intIndex, intIndex,
    DataControlRowType.Separator, DataControlRowState.Normal)
    ' Create a table cell for the row
    Dim cellHeader As New TableCell()
    ' Set the colspan of the cell to the width of the table
    cellHeader.ColumnSpan = Me.GridView1.Columns.Count
    ' Set the text
    cellHeader.Text = "Subheading here"
    ' Add the cell to the row
    gvrSubHeading.Cells.Add(cellHeader)
    ' Add the row to the table
    tblGrid.Controls.AddAt(intIndex, gvrSubHeading)
    End If
    End If
    End Sub

    This is fine for all records except the record for which the subheading row
    is added for (the row after a subheading). I've made an example page for
    testing at:
    http://www.cryptpad.com/gridviewproblem.zip
    Commenting out the code to add subheadings fixes the problem.

    Does anyone know why inserting extra table rows is messing up the
    CommandName and / or CommandArgument?
    Leon Mayne, Apr 22, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    2,263
    GrantMagic
    Sep 28, 2004
  2. =?Utf-8?B?TWlrZQ==?=

    Help ASP.NET app suddenly stopped working

    =?Utf-8?B?TWlrZQ==?=, Feb 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    364
    =?Utf-8?B?TWlrZQ==?=
    Feb 2, 2005
  3. J Rieggle
    Replies:
    1
    Views:
    448
    Curt_C [MVP]
    Jul 21, 2005
  4. RobT
    Replies:
    0
    Views:
    1,146
  5. Replies:
    2
    Views:
    141
Loading...

Share This Page