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
-
Make your changes in client script or initiate a postback to the server to run the server method.
-
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") { // ... } }
Tadas : So, I was a little bit wrong :). Better emulate a custom command, not Select. See my answer. But my previous suggestion should work too. Try to add javascript: before __doPostBack().Tadas : Hmm, I don't know. When you click a row do you get postback? -
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.
0 comments:
Post a Comment