Reputation:
hiiiiiiiii I am trying to do the following : I have a gridview and I want to fire a function(C# function) when I click over the row (any where) this my code :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alertBox = "alert('";
if (e.Row.RowType == DataControlRowType.DataRow)
{
alertBox +=e.Row.Cells[0].Text;
alertBox += "')";
e.Row.Attributes.Add("onclick", alertBox);
}
}
public void test()
{
Response.Write("ffff");
}
this is working ..and every time I click over the gridview I found an alert ...but I want to fire C# function(like test function in the code) how to do that thanks
Upvotes: 1
Views: 2724
Reputation: 38678
To execute a code-behind method with a postback you would have to manually initiate a postback. Essentially you would be wiring up your own event outside of the GridView control because the "RowClick" event doesn't exist - See Tadas' answer.
Alternatively you could execute the code-behind method without a postback. You could use an Ajax callback. See Ajax Page Methods in ASP.Net.
Upvotes: 0
Reputation: 4220
You can emulate GridView command (for example 'SelectCommand') then catch RowCommand event and call your method. Like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + GridView1.ClientID + "','SelectCommand$" + e.Row.RowIndex.ToString() + "')")
...
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectCommand") {
// ...
}
}
Upvotes: 3
Reputation: 7200
Make your changes in client script or initiate a postback to the server to run the server method.
Upvotes: 0