ASP.NET 2.0 problem with a GridView with 2 CommandFields

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,
 
C

Christopher Reed

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.
 
B

ber.janssens

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:
 
F

Flinky Wisty Pomm

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
 
B

ber.janssens

Nevermind, I solved the problem by using two ButtonFields with the
parameter CommandName.

Thanks anyway.
 

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

Top