Click here to Skip to main content
Click here to Skip to main content

Steps to Write GridView PostBack Events

, 7 Jan 2006
Rate this:
Please Sign up or sign in to vote.
This is an article about how to add events programmatically for GridView controls, for selection,deleting,sorting etc.


I was playing with the GridView control and suddenly the question came up in my mind that how could I select a GridView control row while clicking on any area of the GridView control rather than depending on the Select button of the GridView control. Then I Googled for some time but could not find the exact solution. A lot of solutions were available for the selection of a row but I could not find a solution which when I clicked on the GridView, updates the DetailView automatically by grabbing the SelectionIndexChanged event. Following are the results of my search for a solution. Please let me know if this can be done by any other way.

Using the code

First, you need to bind the RowDataBound event to the GridView. Then in that event, write the following code.

In this code, the first line checks if the ItemIndex passed is proper. Then it adds the script for mouse over for the cursor to change to the Hand cursor. In the next line, it adds the postback client event by calling the GetPostBackClientEvent function with two arguments. The first argument contains the GridView object and the second argument contains a string that contains the event and the parameter. Like, if you want to call the SelectedIndexChange event, you need to pass the 'Select' as the event and the RowIndex to select as the parameter. You need to combine the text using $. So if you give 'Select$4' as the second argument, it calls the SelectedIndexChanged event for fourth row as SelectedRow.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.DataItemIndex == -1)

          "Select$" + e.Row.RowIndex.ToString()));

Similarly, the following are the other parameters you can use for calling other events:

  • "Sort${ColumnName}" - Here {ColumnName} can be the DataField of the column you want to sort.
  • "Page${PageNumber}" - Here {PageNumber} is the page number you want to select.
  • "Delete${RowNumber}"- Here {RowNumber} is the row number you want to delete.
  • "Edit${RowNumber}" - Here {RowNumber} is the row number you want to edit.

So you can write any postback client side event by passing the GridView name and the above listed parameter as said. So suppose if I want to change the page number by a control's OnClick event, I simple add a postback reference to the OnClick event of that control like so: e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent({{GRIDVIEWOBJECT} ,"Delete$" + e.Row.RowIndex.ToString()));. The above code simply writes a postback client event's JavaScript on the "OnClick" event of that control. When we click on the control, it calls the delete functionality of the DataGrid.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Manish Pansiniya
Founder IntelliPro Solutions Pvt. Ltd.
India India
A 8 or something in .NET, living in Ahmedabad, India owned IntelliPro Solutions Pvt. Ltd..
Currently working on .NET technologies, MVC and Silverlight.
My little blog is for helping community with the solution for problems or helping them to understand new technology. You can reach to my blog at
To contact me, post comment here or email me at manish AT
Group type: Organisation (No members)

Follow on   Twitter

Comments and Discussions

QuestionPageMethods are neater solution PinmemberManjit Dosanjh11-Aug-12 14:21 
Using pagemethods you can get a neater solution with no need for a postback. It's what AJAX was created for!! Squiffy!
Creating and consuming page methods
"If the facts don't fit the theory, change the facts."

GeneralMy vote of 5 PinmemberAsween26-May-11 7:03 
GeneralProblem with dynamic gridview PinmemberMember 786028326-May-11 5:59 
GeneralRe: Problem with dynamic gridview PingroupManish Pansiniya26-May-11 9:20 
Generalredirection Pinmemberxiaota21-Oct-07 3:08 
Questionhow can i fire the ItemCommand from here? Pinmemberluzer29-Jun-07 10:26 
GeneralDisabling "onclick" event when the edit button is clicked Pinmembernicosiam6-Apr-07 7:28 
GeneralRe: Disabling "onclick" event when the edit button is clicked PinmemberManish Pansiniya6-Apr-07 8:14 
GeneralRe: Disabling "onclick" event when the edit button is clicked Pinmembernicosiam9-Apr-07 9:59 
GeneralRe: Disabling "onclick" event when the edit button is clicked PinmemberManish Pansiniya10-Apr-07 2:16 
GeneralRe: Disabling "onclick" event when the edit button is clicked Pinmembernicosiam10-Apr-07 7:37 
QuestionObject Null in Edit Mode of Grid View Pinmemberitsbijusamuel29-Mar-07 22:23 
AnswerRe: Object Null in Edit Mode of Grid View PinmemberManish Pansiniya30-Mar-07 3:44 
Generalevent for clicking on grid view rows PinmemberNigam SAMir4-Feb-07 18:56 
GeneralRe: event for clicking on grid view rows PinmemberManish Pansiniya4-Feb-07 20:22 
QuestionGreat - So simple, but Firefox ... PinmemberericVV20-Jan-07 0:02 
AnswerRe: Great - So simple, but Firefox ... PinmemberManish Pansiniya20-Jan-07 0:49 
GeneralRe: Great - So simple, but Firefox ... Pinmembersaille19-Apr-07 17:37 
GeneralRe: Great - So simple, but Firefox ... PinmemberManish Pansiniya20-Apr-07 18:56 
GeneralExcellent solution PinmemberPujari12-Dec-06 1:04 
GeneralRe: Excellent solution PinmemberManish Pansiniya12-Dec-06 4:54 
Generalusing this method with dynamically created gridview Pinmemberwendigt1-Nov-06 4:54 
GeneralRe: using this method with dynamically created gridview Pinmemberwendigt1-Nov-06 5:10 
GeneralChecking on validation Problem Pinmemberdpmvpa14-Sep-06 7:54 
GeneralValidation problems Pinmemberdpmvpa13-Sep-06 3:14 
GeneralRe: Validation problems Pinmemberregistrationisformexicans21-Dec-06 5:35 
GeneralRe: Validation problems PinmemberMike Gledhill29-Jun-09 23:31 
GeneralValidation problems Pinmemberdpmvpa3-Sep-06 13:56 
GeneralRe: Validation problems PinmemberManish A Pansiniya10-Sep-06 20:45 
GeneralThankfull Pinmembernarcis050525-Apr-06 23:42 
Generalgreat! Pinmemberbugmenot1238-Jan-06 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 7 Jan 2006
Article Copyright 2006 by Manish Pansiniya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid