GridView CommandArgument suddenly stopped working

L

Leon Mayne

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?
 
L

Leon Mayne

Leon Mayne said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top