Reputation: 1623
Can I call a JS function on the SelectedIndexChanged event of a GridView? I referred these 2 SO posts - Post 1 and Post 2 that address this but they aren't helping.
I tried
OnSelectedIndexChanged="selectedindexchanged()"
however, in return I recieved -
'ASP.default_aspx' does not contain a definition for 'selectedindexchanged' and no
extension method 'selectedindexchanged' accepting a first argument of type
'ASP.default_aspx' could be found (are you missing a using directive or an
assembly reference?)
Can anyone confirm whether this can be done and if it can the right way to do it?
Upvotes: 0
Views: 4255
Reputation: 1890
Use Row Data Bound Event to bind your java script function,
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
e.Row.Attributes.Add("onclick", "selectedindexchanged()");
}
}
Now when you click on a grid view row, the selectedindexchanged
will be called.
Upvotes: 0
Reputation: 75103
OnSelectedIndexChanged
is a server event, not a javascript event.
the Server Control GridView
does not allow, out-of-the-box javascript methods to be hooked on.
You need to extend it and create such methods, or use other Grid Control available as a 3rd party.
What you can do, so you don't mess up to much in the code, is, using jQuery, for example, hook up to all rows and fire an event when something on that row was clicked, holding the return (submission of the form back to the server).
For that, you need to see what's the output HTML and start from there.
This was one of many features that I moved to MVC instead of keep using WebForms, in MVC you have total control in what's happening in your page, and you can do everything by yourself without ever wondering of such things.
Upvotes: 2