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

I´ve been searching for a while now, but cant find the answer for this very simple question!

I have a gridview, which I populate with fields manually.

And then I have this method:

protected void GridViewResults_RowDataBound(object sender, GridViewRowEventArgs e) {
      if (e.Row.RowType==DataControlRowType.DataRow) {
       
      }
}


Now what do I type in there, to make a call to a method in the same codebehind file, with an ID parameter from a column called "ID" ?

For an example this method:

protected void CallMe(int Id) {

}

hope you can help :)
Posted

If you use e.Row.DataItem you'll get the bound object.

[UPDATE]

If your DataSource is List<Employee> then you'll get Employee object using this property. And then get whatever property you want from that object.
 
Share this answer
 
v2
Comments
lvq684 6-Aug-10 7:53am    
Uhm yea ok.. And what do I do from there? (sorry, Im not that much into asp.net yet!)
Arun Jacob 6-Aug-10 8:08am    
Updated answer
try this...
protected void GridViewResults_RowDataBound(object sender, GridViewRowEventArgs e)
      {
      if (e.Row.RowType==DataControlRowType.DataRow) 
      {
         string str="";
         /*if you have "ID" Column as BOUNDCOLUMN*/
         str = e.Row.Cells["ID Column Index"].Text;

         /*else if You have "ID" Column as TEMPLATECOLUMN then You must get the reference to the Control which you have given in "ItemTemplate", i am assuming that you have given Label with 'ID="Label1"' in the ItemTemplate*/
         Label tempLabel = (Label)e.Row.Cells["ID Column Index"].FindControl("Label1")/*finding control with ID in Particular Cell*/

         if(tempLabel!=null)
             str=tempLabel.Text;
         /*Now You got the text to pass in "str" in both types "BOUNDCOLUMN" and "TEMPLATECOLUMN".. just pass it now*/
         CallMe(str);
      }
}
 
Share this answer
 
v4
Comments
lvq684 6-Aug-10 8:19am    
Thanks alot for your answer, very useful!
But I explained myself kinda bad. What I want is when I CLICK the row, to call the function. Not everytime a row is generated :)
koool.kabeer 6-Aug-10 9:13am    
if you want to run a javascript function on Clicking then...

if (e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick","JavascriptFunctionName();");
}
koool.kabeer 6-Aug-10 9:13am    
put this in RowDataBound......
I´ve updated this question.

Here:
http://www.codeproject.com/Questions/99790/Bind-a-call-to-codebehind-method-AND-javascript-jq.aspx

Thanks! :)
 
Share this answer
 

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