GridView question

M

Mike P

I have a grid view with a button field that when clicked I want it to
redirect to another page, passing the primary key of the row with it.
This was a pretty easy thing to do with the DataGrid, but it is so far
proving a pain with the GridView.

Here is my button :

<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/btnDetails.jpg"
CommandName="OpportunityDetails" />

In the GridView DataKeyNames is set like this :

DataKeyNames="OpportunityID"

And I am trying to redirect using this code :

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "OpportunityDetails")
{
//get PK (opportunityID)
int intOppID = Convert.ToInt32(e.CommandArgument);

Response.Redirect("OpportunityDetails.aspx?oppid=" +
intOppID);
}
}

Instead of the PrimaryKey being passed, I just get the GridView index
passed (for the first row 0 is passed, for the second row 1 is passed
etc).

There must be a simple solution to this, I'm hoping somebody can tell me
what it is!
 
E

eguworks

instead of the datakeynames you might want to use selected
gridview1.selectedDataKey or selectedValue
 

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