5,696,576 members and growing! (16,512 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

Steps to Write GridView PostBack Events

By Manish Pansiniya

This is an article about how to add events programmatically for GridView controls, for selection,deleting,sorting etc.
C#, Javascript, Windows, .NET 2.0, .NET, ASP.NET, Visual Studio, VS2005, Dev

Posted: 7 Jan 2006
Updated: 7 Jan 2006
Views: 88,663
Bookmarked: 48 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 4.48 Rating: 3.50 out of 5
3 votes, 15.8%
1
0 votes, 0.0%
2
2 votes, 10.5%
3
3 votes, 15.8%
4
11 votes, 57.9%
5

Introduction

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)
        return;

    e.Row.Attributes.Add("onMouseOver", 
          "this.style.cursor='hand';");
    e.Row.Attributes.Add("onclick", 
          this.GetPostBackClientEvent(GridView1, 
          "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.

License

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


A 29 or something, living in Ahmedabad, India working as Tech Lead.

Currently working on .NET 2.0 and moving on .NET 3.5 with Great Courage Poke tongue.

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 www.pansiniya.com/blog or maniish.wordpress.com.

To contact me, post comment here or email me at manish AT pansiniya.com
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
Generalredirectionmemberxiaota3:08 21 Oct '07  
Questionhow can i fire the ItemCommand from here?memberluzer10:26 29 Jun '07  
GeneralDisabling "onclick" event when the edit button is clickedmembernicosiam7:28 6 Apr '07  
GeneralRe: Disabling "onclick" event when the edit button is clickedmemberManish Pansiniya8:14 6 Apr '07  
GeneralRe: Disabling "onclick" event when the edit button is clickedmembernicosiam9:59 9 Apr '07  
GeneralRe: Disabling "onclick" event when the edit button is clickedmemberManish Pansiniya2:16 10 Apr '07  
GeneralRe: Disabling "onclick" event when the edit button is clickedmembernicosiam7:37 10 Apr '07  
QuestionObject Null in Edit Mode of Grid Viewmemberitsbijusamuel22:23 29 Mar '07  
AnswerRe: Object Null in Edit Mode of Grid ViewmemberManish Pansiniya3:44 30 Mar '07  
Generalevent for clicking on grid view rowsmemberNigam SAMir18:56 4 Feb '07  
GeneralRe: event for clicking on grid view rowsmemberManish Pansiniya20:22 4 Feb '07  
QuestionGreat - So simple, but Firefox ...memberericVV0:02 20 Jan '07  
AnswerRe: Great - So simple, but Firefox ...memberManish Pansiniya0:49 20 Jan '07  
GeneralRe: Great - So simple, but Firefox ...membersaille17:37 19 Apr '07  
GeneralRe: Great - So simple, but Firefox ...memberManish Pansiniya18:56 20 Apr '07  
GeneralExcellent solutionmemberPujari1:04 12 Dec '06  
GeneralRe: Excellent solutionmemberManish Pansiniya4:54 12 Dec '06  
Generalusing this method with dynamically created gridviewmemberwendigt4:54 1 Nov '06  
GeneralRe: using this method with dynamically created gridviewmemberwendigt5:10 1 Nov '06  
GeneralChecking on validation Problemmemberdpmvpa7:54 14 Sep '06  
GeneralValidation problemsmemberdpmvpa3:14 13 Sep '06  
GeneralRe: Validation problemsmemberregistrationisformexicans5:35 21 Dec '06  
GeneralValidation problemsmemberdpmvpa13:56 3 Sep '06  
GeneralRe: Validation problemsmemberManish A Pansiniya20:45 10 Sep '06  
GeneralThankfullmembernarcis050523:42 25 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jan 2006
Editor: Smitha Vijayan
Copyright 2006 by Manish Pansiniya
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project