Click here to Skip to main content
15,888,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

When I use a autogenerated select button in gridview and click on it, the event GridView_selected index changed gets fired.

But I want to fire this event on row click and not on select button click.

How can I Proceed??
Posted

Please refer my answer - Data GridView selected index changed firing Problem ASP.NET[^].

Update
It seems that without the Select Button, the event is not going to fire. So, do like below.

  1. Make the AutoGenerateSelectButton = "True".
    And write OnSelectedIndexChanged Event.
    HTML
    AutoGenerateSelectButton="True"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
  2. Add the below Code in RowDataBound Event.
    C#
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Style.Add(HtmlTextWriterStyle.Display, "none");
    }
    
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].Style.Add(HtmlTextWriterStyle.Display, "none");
    
        e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }


Now it should work perfectly and Select button will not show.
 
Share this answer
 
v4
Comments
aks.shan 17-Jul-13 5:30am    
I saw your answer.
i have updated onrowdatabound="GridView1_RowDataBound" event accordingly but this works properly only when i set autogenerateselectbutton as true and not otherwise.

I dont want the select button to be displayed in the grid.
aks.shan 5-Aug-13 5:49am    
Hi Tadit,
one more improvisation.
I have a hyperlink column in the same grid as discussed earlier.
On the click of hyperlink,the row click event is fired which we did earlier.
I want the page to be redirected on hyperlink click without the rowclick attribute getting fired.

I tried to remove the attribute on the cell in which hyperlink is there. but its not working.
-Akshay.
Are you sure that without the button it's not working?
I mean the code inside the RowDataBound event actually adds a onclick event for the row.
But I have not tested that code. Please let me know... If it is not working, then I will try to check and resolve this at my end.

Thanks...
As we have a click event on the row, so I guess it will be fired, if you click anywhere on the row. If you see the html generated, the onclick attribute is added to the row, not to the individual cell, so removing the attribute from the cell will not work.

So, you can do one thing... On that SelectedIndexChanged, check something as a part of your redirection logic and redirect from that event itself. Or is there any problem?
aks.shan 17-Jul-13 6:06am    
yes, Its not working without the button.
Please do let me know if u find a solution for this.
Thanks.
Take out "selected index changed" event and add Row_Command event.
this event will even fire if you click "Select" button..hope this helps you..


loop through the gridview rows from javascript and find out the clicked row(tr). and fire a click event for hidden buton..
 
Share this answer
 
v3
Comments
aks.shan 17-Jul-13 6:11am    
Can u send me a sample code of this.
Thanks.
vinay.tatipamula 17-Jul-13 13:54pm    
Tadit Dash's solution is good. my solution is not straight forward.. thanks.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900