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

I have a Gridview, and I want to bind each row to both call a javascript function, AND a codebehind method, with a parameter from one of the gridview columns, after each other.
I´m new to asp.net, so bare over with me :)

This is my RowDataBound method that executes when rows are generated:

protected void GridViewResults_RowDataBound(object sender,GridViewRowEventArgs e) {
      if (e.Row.RowType==DataControlRowType.DataRow) {
        //What do I do here?
      }


And my javascript function:

function hello() {
alert("hello there!");
}


And my codebehind method:
public void Callme(int GridviewColumnId) {

}


so when you click a row in the gridview, these methods/functions are called.

Hope you can help! Thanks alot :)
Posted
Comments
koool.kabeer 6-Aug-10 9:18am    
if you want to run a javascript function on Clicking then...

if (e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick","JavascriptFunctionName();");
}
and to call the codebehind method do a postback from javascript function "hello()" where ever in the funtion
lvq684 6-Aug-10 9:45am    
Thanks for your answer.
I would like to avoid any postbacks. Is this possible?

1 solution

As Koool.Kabeer has already suggested:
C#
protected void GridViewResults_RowDataBound(object sender,GridViewRowEventArgs e) 
{  
   if(e.Row.RowType==DataControlRowType.DataRow) 
   { 
       e.Row.Attributes.Add("onclick","JavascriptFunctionName("+ myColumnValueAsParameterToJS +");"); 
   }
}


This way, you bind the javascript to the click event of the row. There would be no postback when you do so.
Try!

UPDATE 1:
Have a look at this article, integrate it.
Clickable Rows in a DataGrid[^]
 
Share this answer
 
v2
Comments
lvq684 6-Aug-10 11:53am    
Yea, that solves the javascript part. But as described I also want a codebehind method to be called when you click on a row. So BOTH methods will be called at "the same time".

Thanks for your time :)
Sandeep Mewara 6-Aug-10 12:20pm    
Answer updated...

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