I want to Display an alter message with DataGrid Button.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have read an article on the web. And There is the source.
I get an error when I'm using this:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.Item)
{
Button btnDelete = (Button) e.Item.FindControl("Delete");
btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you
sure you want to delete this record?');");
}
}

Aspx
<asp:ButtonColumn Text="Remove" ButtonType="PushButton" CommandName="Delete">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="50px"></ItemStyle>
</asp:ButtonColumn>

Who can tell me why this code doesnt work?
 
Hi,

FindControl() method needs the id of the control to be found. In this case,
since there is no ID associated with the button (in the button column),
FindControl will not work. Instead, try this approach:

Button btnDelete = (Button)(e.Item.Cells(<button column
index>).Controls(0));
btnDelete.Attributes.Add (...);

HTH

I have read an article on the web. And There is the source.
I get an error when I'm using this:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.Item)
{
Button btnDelete = (Button) e.Item.FindControl("Delete");
btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you
sure you want to delete this record?');");
}
}

Aspx
<asp:ButtonColumn Text="Remove" ButtonType="PushButton"
CommandName="Delete">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="50px"></ItemStyle>
</asp:ButtonColumn>

Who can tell me why this code doesnt work?
 
Back
Top