Just like Bob said, the easiest/quickest way to get the index of the clicked row (in case you need the number for something else) is to add an attribute to the TR elements - you can then query this value in your onclick handler.
Drawing on the code I wrote earlier, here's a dynamic solution.
/* this points to the clicked table row */
var clickedRow = this;
// get the first input item that's a child of the clicked row
var checkBox = clickedRow.getElementsByTagName('input');
checkBox.checked = (!checkBox.checked);
alert('Index of clicked row: ' + clickedRow.getAttribute('rowIndex'));
// *** 'this' points to the row that was clicked ***
// make a new table-cell element
td = document.createElement('td');
// make a new input element
btnInput = document.createElement('input');
btnInput.setAttribute('value', 'click me');
btnInput.onclick = myBtnClicked;
// place the btn inside the new table cell
// place this new cell inside the row that was clicked
// remove the onclick handler from the row, to prevent adding a button multiple times
this.onclick = null;
// attach the specified function to the onclick event of all 'tr' elements in the document
var i, num;
var tableRows = document.getElementsByTagName('tr');
num = tableRows.length;
for (i=0; i<num; i++)
tableRows[i].onclick = functionToAttach;
// Sample onInit function.
// make sure you only use one function or the other here
// the row will just tell us that we've clicked it
// the row will add a cell that contains a button, whenever the row is clicked
// try commenting out the above line of code and uncommenting this one instead
for ($i=0; $i<10; $i++)
echo"<tr rowIndex='$i'><td><input type='checkbox' id='cb_$i'></td><td>click to toggle checkBox</td></tr>";
Beautiful! I can think of many places where this would be useful. Bravo on your explanation and great example! Clean, concise, and well documented. I don't usually get to do too much web development, but just spent a week immersed in it adding features to a legacy ASP application. It never fails to amaze me how much can be handled on the clent side. I think you might only be limited by your imagination.
Hi, I want to make ref of jquery.js through c# code and then want to create a function in C# to which i want to pass object as parameter. That function will do tasks that JQuery do. f.g Button as object and do animations on button using Jquery with C#(function). Thanks in advance.
Hi, I am an intermediate user with JQuery.
Actually I want to create a class in c# that will handle Jquery functionality.
My purpose regarding this is :
1> If someone dont know jquery then one can use my C# funtions for animation or other tasks that jquery has. My class will call jquery functions and ll return output.
2> Easy to debug the code using vs-degugger.
I write server control dll's, in which I embed scripts, and load the appropriate ones on demand, during the page.init life cycle. And then I create controls, that wire up to the scripts. When the page loads, everything is functional.
If you just had a pin-point question, with a code sample, then that's easy to answer, but in your case, your looking for someone to engineer it for you, and I don't think a class is sufficient for your needs.
I don't have an answer to offer, because I think the concept is flawed, or it exist on a super advanced level of programming.
I don't think you understand how ASP.net works. This isn't entirely your fault because ASP.net does its best to hide the client/server nature of websites.
Back end (C#) code is executed on the server. Its purpose is to generate markup which is sent to the browser, which can include scripts and links to scripts (e.g. jQuery). Once a request has been handled and markup sent back to the browser, your back end code is not running any more.
The only ways to communicate between them are HTTP requests, either through loading a new page with an <a> or <form action=...> tag, or via an AJAX request.