mellomel70
Programmer
Hi - I'm working in ASP.Net 2.0. My page has a GridView control, which is populated by a SQLDataSource. The GridView has one Button control per row. The button has a client-side javascript function (ShowDelete) attached to its OnClientClick event on DataBinding of the GridView. This event fires some javascript. The javascript does 3 things:
- It changes the text of the button from "Hide" to "Create"
- It changes the background color of the button from red to green
- It assigns a new onClientClick event to the button: "ShowAvailable"
The text and the color of the button change with no problem. After I change the OnClientClick event, I send an alert with the button's OnClientClick event and it reports it to be "ShowAvailable". However, when I click on the button, it doesn't fire ShowAvailable, it fires the original onClientClick event (ShowDelete). Also, when I do View Source, it shows the button's text, color and OnClientClick event as their original settings, i.e., "Hidden", "Red" and "ShowDelete", even though I'm looking at a green button that says "Create." What's going on here?
Here's the code I use to attach the OnClientClick event on DataBinding of the GridView:
- It changes the text of the button from "Hide" to "Create"
- It changes the background color of the button from red to green
- It assigns a new onClientClick event to the button: "ShowAvailable"
The text and the color of the button change with no problem. After I change the OnClientClick event, I send an alert with the button's OnClientClick event and it reports it to be "ShowAvailable". However, when I click on the button, it doesn't fire ShowAvailable, it fires the original onClientClick event (ShowDelete). Also, when I do View Source, it shows the button's text, color and OnClientClick event as their original settings, i.e., "Hidden", "Red" and "ShowDelete", even though I'm looking at a green button that says "Create." What's going on here?
Here's the code I use to attach the OnClientClick event on DataBinding of the GridView:
Code:
foreach (GridViewRow row in this.gvLocation.Rows)
{
if (row.RowType == DataControlRowType.DataRow || row.RowType == DataControlRowType.EmptyDataRow)
{
int rowindex = row.RowIndex + 1; //to exclude the header which is counted as a row when we get to javascript
btnAction.OnClientClick = "ShowDelete(" + rowindex.ToString() + "); return false";
}
}
[\code]
Here's the javascript for ShowDelete:
[code]
function ShowDelete(rowindex)
{
var locationgridID = '<%= gvLocation.ClientID %>';
locationgrid = document.getElementById(locationgridID);
if (locationgrid != null)
{
action_cell = locationgrid.rows[rowindex].cells[3];
for (k = 0; k < action_cell.childNodes.length; k++)
{
if (typeof(action_cell.childNodes[k].id) == 'string')
{
action_cell.childNodes[k].value = "Create";
action_cell.childNodes[k].style.backgroundColor = "Green";
action_cell.childNodes[k].onclientclick = "ShowAvailable(" + rowindex + "); return false";
alert(action_cell.childNodes[k].onclientclick); // reports the OnClientClick event is ShowAvailable
}
}
}
}
[\code]
Any thoughts are greatly appreciated!