Hi DotNetSlackers,
I was wondering if anyone knew how to dynamically append an event handler/function to a specific GridViewRow's "delete" command and have it execute after the "delete" command executes. I apologize if how I'm wording this is confusing people, but I'll try to explain as best as I can.
Let's assume that you have a GridView with the following structure:
<ajax:GridView id="gridView" runat="server" DeleteCommandEvent="onDeleteCommand">
<Columns>
<ajax:GridViewCommandColumn ButtonType="Link" ShowDeleteButton="true"/>
<ajax:GridViewBoundColumn HeaderText="Test Val 1" DataField="FirstItem"/>
<ajax:GridViewBoundColumn HeaderText="Test Val 2" DataField="SecondItem"/>
</Columns>
</ajax:GridView>
When populated, it has, say, 5 rows of data. I would like to append another event handler/function to a specific row's "OnDeleteCommand". It could be any row, but for this example, let's append the event to the 3rd row's delete command. Would there be any way that the AjaxDataControls framework would allow me to do this?
One alternative I've thought of to achieve this means is to have a "GridViewTemplateColumn" instead of a "GridViewCommandColumn" and place an image inside of the templated column. During the GridView's onRowDataBind event, I could grab that image and set the "onDeleteCommand" to the image's "onclick" event, as well as append other functions/event handlers. General idea below:
<ajax:GridView id="gridView" runat="server">
<Columns>
<ajax:GridViewTemplateColumn>
<ItemTemplate>
<img id="imgDelete" src="/Images/delete.gif" />
</ItemTemplate>
</ajax:GridViewTemplateColumn>
<ajax:GridViewBoundColumn HeaderText="Test Val 1" DataField="FirstItem"/>
<ajax:GridViewBoundColumn HeaderText="Test Val 2" DataField="SecondItem"/>
</Columns>
</ajax:GridView>
<script>
var _gridView;
function pageLoad()
{
_gridView = $find('<%= gridView.ClientID %>');
_gridView.add_rowDataBound(onDataBound);
}
function onDataBound(sender, e)
{
var row = e.get_row();
var imgDelete = row.findControl('imgDelete');
imgDelete.attachEvent('onclick',
function(){
onDeleteCommand(this, row.get_index());
});
if (/*certain conditions are met*/)
{
imgDelete.attachEvent('onclick',
function(){
someEvent(/*pass in parameters here*/)
});
}
}
function onDeleteCommand(sender, rowIndex){/*handles row deletion*/}
function someEvent(/*function parameters */){/*does something*/}
</script>
Would anyone have any insight as to how to go about this? Any information would be most appreciated.