I've been trying to work on this for awhile now, and I'm at my wits end. I have a web form with a GridView control. The data source is a dynamically created DataTable, and I would like to do this without Template Fields if at all possible. My goal is to have 4 checkbox columns that the user can click to change the values. I just made the 4 columns data type bool so that they automatically show up as checkboxes. I know how to select a row with a little help from javascript, but being able to detect which column was clicked has thus eluded me.
Relevant code showing how I am currently selecting the row:
function getSelectedRow(row)
{
jQuery(row).children(":first").children(":first")[0].click();
}
Code Behind:
protected void gvReviewOrder_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string onClick = "javascript: getSelectedRow(this);";
e.Row.Attributes["onclick"] = onClick;
e.Row.Style["cursor"] = "pointer";
}
}
The latest thing I have tried was using JQuery from the following thread: Table row and column number in jQuery
Instead of using an alert, I was trying to update the clicked column index into a hidden field, so this is what my current Javascript function looks like:
$("td").click(function ()
{
var colIndex = $(this).parent().children().index($(this));
$("#hfColumnId").val(colIndex);
});
So hypothetically my hidden field "hfColumnId" should be updated when the cell is clicked, but that is not happening. Do I need to add code on my OnRowDataBound Event to add the click event first?
As you can probably guess, I'm still learning when it comes to web forms, and I'm just over my head right now. Any help would be much appreciated.
So to make a long question short, is there any way to return the column index when a cell is clicked via jquery from a GridView control?
Thanks in advance!
edit I found this thread from last year with a solution, but they suggest setting EnableEventValidation to false, and I know this is not recommended. So if I could figure out how to implement that way without setting that to false it could be a potential solution maybe?
Get clicked cell index in gridview (not datagridview) asp.net