Confirming Delete for a asp:ButtonColumn in asp:datagrid..

R

RSB

Hi Everyone,
i am using a ASP: DataGrid control in the page and for each row i have a
Delete Button Column. Every thing works fine.
But now i want to add a Java script confirmation check for the user that
Delete has been selected and are they sure to continue.

I have already used this Attributes.Add (
cmdDelete.Attributes.Add("onClick", "return confirmDelete();")

)

command for a Single delete button on the page but don't understand how to
add some thing same for the dataGrid Button.

Please help.

Thanks
RSB
 
J

John Oakes

For the template column...

<asp:TemplateColumn>
<ItemTemplate>
<span onclick="unlockChild(this, 'delete');">
<asp:LinkButton runat="server" Text="X" CommandName="Delete"
CausesValidation="false"></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateColumn>

and the corresponding JavaScript function...

<script language="javascript">
<!--
function unlockChild(parentObj, action){
var allowClick = confirm('Are you sure you want to ' + action + ' this
item?');
window.event.returnValue = allowClick;
return allowClick;
}
//-->

-John Oakes
 
A

Ashish M Bhonkiya

Hi RSB,

you need to do it in DataGrid1_ItemCreated
get the (button) control instance by using the findcontrol method and then
attach the script using control.Attributes.Add method.

Button myButton = (Button) this.findControl("btnDelete")
myButton.Attributes.Add("onclick",return confirmDelete();");


Regards
Ashish M Bhonkiya
 
P

Prodip Saha

Asish is right but I found in certain instances the ItemCreated is not
reliable. I used ItemDataBound of the datagrid to accomplish this.

this.dgGrid1.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgGrid1_ItemDataBoun
d); //put this line of code in the InitializeComponent method or in the
PageLoad event

private void dgGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
try
{

if(e.Item.ItemType==ListItemType.Item
||e.Item.ItemType==ListItemType.AlternatingItem)
{

LinkButton lbtn=(LinkButton)e.Item.FindControl("btnDelete");

if(lbtn!=null)
{

lbtn.Attributes.Add("onclick","return confirm('Are you sure you want to
delete?');");

}

}


}
catch(Exception ex)
{
throw ex;
}

}

Prodip
 
P

Prodip Saha

Please add the deligate method inside the InitializeComponent method. This
method executes before page load.
Prodip
Lane said:
I tried this solution Prodip sugeested and got a error regarding the
ItemDataBound in the page load area???
 
K

Karim

ItemDataBound in the page load area???

This is a sample in the itemcreated event for the grid
......
ListItemType elemType = e.Item.ItemType;
if (elemType == ListItemType.Item || elemType
=ListItemType.AlternatingItem)
{
//add code to get button instance

button.Attributes.Add ("onclick",
"return confirm ('Are you sure?');");

}
....


Karim
 

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