Migrating ASP.NET 2.0 B2 to final bits PART III Rowcommand

I described in a earlier post how to access a row in a gridview during the rowcommand event.

http://weblogs.asp.net/hpreishuber/archive/2005/08/05/421712.aspx

This doesnt work any longer, or -better- produces following error message

Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.GridViewRow'.

So instead of doing some parent parent adventures use now

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)

Select Case e.CommandName

Case "profil"

Dim row As GridViewRow = GridView1.Rows(CInt(e.CommandArgument))

 

Published Thursday, November 10, 2005 10:05 AM by preishuber

Comments

# re: Migrating ASP.NET 2.0 B2 to final bits PART III Rowcommand

Thursday, November 10, 2005 9:46 AM by Ron Buckton
The frustrating thing is that the Row property of GridViewCommandEventArgs is marked "internal" for some odd reason.

You could use reflection to get the internal "Row" property, or you can do some subclassing to make it public

public class GridViewEx : GridView
{
  protected override GridViewRow CreateRow(...)
{
return new GridViewRowEx(...)
}
}

public class GridViewRowEx : GridViewRow
{
public GridViewRowEx(...) : base(...)
{
}
protected override bool OnBubbleEvent(...)
{
if(e is CommandEventArgs)
{
GridViewCommandEventArgsEx args =
new GridViewCommandEventArgsEx(this, source, (CommandEventArgs)e);
base.RaiseBubbleEvent(this, args);
return true;
}
return false;
}
}

public class GridViewCommandEventArgsEx :
GridViewCommandEventArgs
{
private GridViewRow _row;
public GridViewCommandEventArgsEx(GridViewRow row, object cs, CommandEventArgs orig)
: base(row, cs, orig)
{
_row = row;
}
public GridViewRow Row
{
get { return _row; }
}
}

You can then add a <tagMapping> in your web.config to remap <asp:GridView> to your custom type and then just try to cast GridViewCommandEventArgs to GridViewCommandEventArgsEx in the RowCommand.

Reflection would be less code but slow, while this override is more code but fast (all early-bound)

Leave a Comment

(required) 
(required) 
(optional)
(required)