Click here to Skip to main content
11,581,982 members (76,021 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi all,

I have a gridview there I placed some bound field and also added an image button in the last column of the grid as template field.

What I want is when I click on the image button my page should redirect to a new page with one of column values, say the id of the row where I clicked.

I can use session or query string for redirecting but how to get the row values?

I tried in many ways but I get a null reference error.
I'm unable to get the selected row using an image button.

How can I code the image button to select a row and get its values?
Please help me.
Thanks in advance...
Posted 19-Aug-10 2:44am
Edited 25-Aug-10 22:34pm
Dalek Dave433.2K
v3
Comments
Dalek Dave at 26-Aug-10 4:35am
   
Edited for Readability, Grammar and Syntax.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

go to selection changing event and write the code below:
Selection changeing Event

int index=gridvview.selectedindex;
session["id"]=(gridview.rows[index].cells[0].text).tostring();

in this way you get all your cell's values from selected row
  Permalink  
v3
Comments
aswathy.s.88 at 26-Aug-10 4:29am
   
Thanks for your help but this is not working....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try exploring more on this MSDN Article[^].

Basically, you need to get the index of the row where the button is located. You can then get the specific row using that index:

GridViewRow row = ContactsGridView.Rows[index];

You can then get the content of the column that you want, for example:

String s = row.Cells[2].Text;

The variable s will have the value of third cell of the selected row (where your button was clicked) of you gridview.

There is an example on the link I shared.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

One way is to add Click event for that image button and get the row as,

GridviewRow gvRow = (sender as ImageButton).NamingContainer as GridviewRow;

and then access the DataKey or ID which you want to pass.
PS: Code is not tested.

Alternatively you can use RowCommand.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Set the DataKeyNames of the grid view of the column name (say the ID from the DB which is in the Result set).

Now in the SelectedIndexChanged event handler writ the following code

int selectedIndex = GridView1.SelectedIndex;
string customID = (GridView1.DataKeys[selectedIndex]["pkCityId"]).ToString();
//now write Ur custom logic for creating the URL and redirecting to the
//new url

simple isn't ???Cool | :cool:

For clarity the name of my Gridview is GridView1 and the DataKeyNames (the column which actually holds the ID value) is pkCityID.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Set commandArgument like this
<asp:ImageButton ID="ibtnEdit"  runat="server" CommandArgument='<%# Container.DataItemIndex+1 %>' />
then in gridview rowcommand event
dim intRow as integer = CInt(e.CommandArgument)
then by using that row number u can get the row values
 gridview.Rows(intRow).Cells (0).Text 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

On image button click

GridViewRow selectedRow = ((ImageButton)sender).Parent.Parent as GridViewRow;
        ImageButton imageButton = (ImageButton)selectedRow.FindControl("imgBtn");
          if (imageButton != null)
          {
            //add your logic here
          }
 

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Hi Friends!

Here's a way to get the values of each cell in a Grids' row (in case of binding data from code).
Consider a Button Field ('Select') in this case and give its CommandName to Select
Write in Row_Command event
if (e.CommandName == "select")
 { 
  //code here
}
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Write this code in selected_index_changed function defile datakey property of gridview then write below code:
Session["updaterec"] = GridView1.SelectedDataKey [0].ToString ();
Response.Redirect("your desire page");
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 320
1 Sergey Alexandrovich Kryukov 185
2 virusstorm 109
3 Frankie-C 105
4 F-ES Sitecore 85
0 OriginalGriff 1,273
1 Sergey Alexandrovich Kryukov 900
2 Abhinav S 617
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 3 Feb 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100