I'm working in Asp.Net and I want to fire SelectedIndexChanged event of a DataGrid by clicking a row, NOT by clicking DataGrid's Select Button. My purpose is getting sixth cell's value of selected row.
How Can I do ?
Firstly, you will need to enable row selections for your datagrid. this will place the proper javascript in your page for what is to come. This will place a select button on your page, but you can convert that column into a template and make the linkbutton
visible=false.
you need a OnRowCreated event handler, so add this to your gridview's source (OnRowCreated="GridView1_RowCreated") or just set up a handler through the gridviews Events Property window.
The event handler will look something like this:
int RowCount = 0;
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// get the row index, add onclick attribute for javascript:__doPostBack('GridView1','Select$0')
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + ((GridView)sender).ID + "','Select$" + (RowCount - 1).ToString() + "')");
e.Row.Attributes.Add("onmouseover", "JavaScript:this.style.cursor='hand';");
}
RowCount++;
}
From my site: http://pixelsyndicate.com/PS/Home/tabid/36/EntryID/33/Default.aspx
Please click 'Mark as Answer' if reply assisted you.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
Two remarks to pixelsyndicate:
If you don't have a Select-button, you will also have to call RegisterForEventValidation for each row in the Render (override) of the page.
The cursor should be pointer, not hand. Hand is old-style IE. Pointer is the official version of it.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Two remarks to pixelsyndicate:
If you don't have a Select-button, you will also have to call RegisterForEventValidation for each row in the Render (override) of the page.
The cursor should be pointer, not hand. Hand is old-style IE. Pointer is the official version of it.
I suggest you make it selectable and just make the column for the select button hidden... then you can avoid all that mucking about with the RegisterForEventValidation
I agree with the point using a 'pointer' not a 'hand'. I have been using that script for a couple years and so it's not so up-to-date :) + corporate users I build for tend to be forced into using IE.
Please click 'Mark as Answer' if reply assisted you.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
umitcelik
Member
6 Points
20 Posts
firing GridView1_SelectedIndexChanged event?
Sep 10, 2009 01:54 PM|LINK
I'm working in Asp.Net and I want to fire SelectedIndexChanged event of a DataGrid by clicking a row, NOT by clicking DataGrid's Select Button. My purpose is getting sixth cell's value of selected row.
How Can I do ?
pixelsyndica...
Star
7826 Points
1344 Posts
Re: firing GridView1_SelectedIndexChanged event?
Sep 10, 2009 02:29 PM|LINK
TO SELECT A ROW ONCLICK:
Firstly, you will need to enable row selections for your datagrid. this will place the proper javascript in your page for what is to come. This will place a select button on your page, but you can convert that column into a template and make the linkbutton visible=false.
you need a OnRowCreated event handler, so add this to your gridview's source (OnRowCreated="GridView1_RowCreated") or just set up a handler through the gridviews Events Property window.
The event handler will look something like this:
int RowCount = 0; protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { // get the row index, add onclick attribute for javascript:__doPostBack('GridView1','Select$0') if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + ((GridView)sender).ID + "','Select$" + (RowCount - 1).ToString() + "')"); e.Row.Attributes.Add("onmouseover", "JavaScript:this.style.cursor='hand';"); } RowCount++; }From my site: http://pixelsyndicate.com/PS/Home/tabid/36/EntryID/33/Default.aspx
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
superguppie
All-Star
48225 Points
8679 Posts
Re: firing GridView1_SelectedIndexChanged event?
Sep 11, 2009 12:41 PM|LINK
If you don't have a Select-button, you will also have to call RegisterForEventValidation for each row in the Render (override) of the page.
The cursor should be pointer, not hand. Hand is old-style IE. Pointer is the official version of it.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
pixelsyndica...
Star
7826 Points
1344 Posts
Re: firing GridView1_SelectedIndexChanged event?
Sep 11, 2009 01:28 PM|LINK
I suggest you make it selectable and just make the column for the select button hidden... then you can avoid all that mucking about with the RegisterForEventValidation
I agree with the point using a 'pointer' not a 'hand'. I have been using that script for a couple years and so it's not so up-to-date :) + corporate users I build for tend to be forced into using IE.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams