I want to Display an alter message with DataGrid Button.

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?
 
S

Shiva

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top