I am having problems with trying to add a JavaScript Onclick function to a DataGrid's Delete function - the Delete otherwise works. I am new to C#/ASP.NET - any idea why this does not work?
Thanks in advance for any help or clues.
The Delete Button is a template column in the DataGrid:
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server"
Text="Delete"
CommandName="Delete"
CausesValidation="false">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
public void DataGrid1_ItemDataBound (object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem )
{
Button b = (Button)e.Item.FindControl("Delete"
string n = (string)DataBinder.Eval (e.Item.DataItem, "blahblah"
string js = "return confirm('Are you sure you want to delete ' + n + '?');";
b.Attributes.Add("onclick ", js);
}
}
The ItemDataBound event raises this error:
System.NullReferenceException: Object reference not set to an instance of an object.
b.Attributes.Add("onclick ", js);
Thanks in advance for any help or clues.
The Delete Button is a template column in the DataGrid:
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server"
Text="Delete"
CommandName="Delete"
CausesValidation="false">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
public void DataGrid1_ItemDataBound (object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem )
{
Button b = (Button)e.Item.FindControl("Delete"
string n = (string)DataBinder.Eval (e.Item.DataItem, "blahblah"
string js = "return confirm('Are you sure you want to delete ' + n + '?');";
b.Attributes.Add("onclick ", js);
}
}
The ItemDataBound event raises this error:
System.NullReferenceException: Object reference not set to an instance of an object.
b.Attributes.Add("onclick ", js);