J
js
I have a Datagrid control on my page. I want to attach a client
JavaScript during the control's ItemCreated event in the CodeBehind
like the following, but I can only attach the script to the Edit
command button, but not the Cancel or Update button. Thanks for your
help.
private void ItemsGrid_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
WebControl button = (WebControl)e.Item.Cells[1].Controls[0];
button.Attributes.Add("OnClick", "return confirm(\"Abandoing edit,
are you sure?\");");
}
}
Here is the html of the Datagrid.
<asp:datagrid id="ItemsGrid" runat="server" AllowPaging="True"
DataKeyField="IntegerValue">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"/>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
HeaderText="Editable" CancelText="Cancel" EditText="Edit"/>
</Columns>
</asp:datagrid>
JavaScript during the control's ItemCreated event in the CodeBehind
like the following, but I can only attach the script to the Edit
command button, but not the Cancel or Update button. Thanks for your
help.
private void ItemsGrid_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
WebControl button = (WebControl)e.Item.Cells[1].Controls[0];
button.Attributes.Add("OnClick", "return confirm(\"Abandoing edit,
are you sure?\");");
}
}
Here is the html of the Datagrid.
<asp:datagrid id="ItemsGrid" runat="server" AllowPaging="True"
DataKeyField="IntegerValue">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"/>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
HeaderText="Editable" CancelText="Cancel" EditText="Edit"/>
</Columns>
</asp:datagrid>