ASP.NET 2.0 problem with a GridView with 2 CommandFields

  • Thread starter Thread starter ber.janssens
  • Start date Start date
B

ber.janssens

Hi,

In ASP.NET 2.0, I have a GridView with 2 CommandFields, how can I
determine in the SelectedIndexChanged event which CommandField is
clicked?

<asp:GridView ID="gvOrderList" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanged="gvOrderList_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="Agent" SortExpression="Agent" />

</Columns>
</asp:GridView>

Can anybody solve this problem?
kind regards,
 
Why do you have two Select CommandFields? The purpose of a Select
CommandField is to select the whole row. Please elaborate further as to
what you want to accomplish.
 
I need to be able to make the difference between the two commandfields
because the GridView with the two Commandfields is a master GridView
with two different slave GridViews. It is the click on a CommandField
button that shows one of the two slave GridViews.

How am I able to solve this problem?

Kind regards,
Bert Janssens




Christopher Reed schreef:
 
Use the commandName property and a ButtonField rather than a
CommandField

<asp:GridView runat="server" OnRowCommand="RowCommandHandler">
<asp:ButtonField CommandName="Select1" Text="Select" />
<asp:ButtonField CommandName="Select2" Text="Select" />
</asp:GridView>

then in your codebehind use

protected void RowCommandHandler(object sender, GridViewRowEventArgs e)
{
if(e.CommandName = "Select1")
{
// Do selects for the first datagrid
}
else
{
// Do selects for the second datagrid
}
}


You can get a reference to the row which fired the event with

protected void RowCommandHandler(object sender, GridViewRowEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = (sender as GridView).Rows[rowIndex];
}


or set the CommandArgument yourself as part of your databind. If I'm
doing something like this, I'll usually set the CommandArgument of my
(x)Button to the primary key of the child record I want to select, so I
can do

protected void RowCommandHandler(..)
{
int childKey = Convert.ToInt32(e.CommandArgument);
if(e.CommandName="FirstCommand")
{
SelectSomeDataForTheFirstCommand(childKey);
}
}

HTH,

-- Flinky Wisty Pomm
 
Nevermind, I solved the problem by using two ButtonFields with the
parameter CommandName.

Thanks anyway.
 
Back
Top