DataList and Prompting User To Confirm Prior to PostBack

J

Jeff S

I have a DataList that includes a <table> definition in the <ItemTemplate>.
The <table> includes an asp:Button that, when clicked, causes a postback
during which some data is deleted (it's a button labeled "Delete"). The
<table> also includes a second button that, when clicked, enables the user
to edit data (although not using an <EditItemTemplate>). When the user
clicks the Delete button, I want to prompt the user to confirm that they
want to delete the data before the postback occurs. The following line works
great for asp:Buttons that are not included in a DataList.

btnDeleteWidget.Attributes.Add("onclick", "return confirm('Delete selected
widget?');");

This line of code serves to pop up the message when the user clicks any
button being rendered as part of the DataList (in my case the same
message/prompt appears when the user clicks either the Edit or the Delete
buttons):

MyDataList.Attributes.Add("onclick", "return confirm('Delete selected
widget?');");

But what I want is for the message box/prompt to appear only when the user
clicks on the Delete button in the DataList (and to not appear when the user
clicks the Edit button that is also in the DataList). Any suggestions for
how to accomplish this?

Thanks!
 
G

Guest

I am pretty sure there is a way as I have checked MSDN that DataList has these two public (server) events

DeleteComman
EditComman

Have you tried reaching the Edit/Delete control by doing this: MyDataList.Controls

----- Jeff S wrote: ----

I have a DataList that includes a <table> definition in the <ItemTemplate>
The <table> includes an asp:Button that, when clicked, causes a postbac
during which some data is deleted (it's a button labeled "Delete"). Th
<table> also includes a second button that, when clicked, enables the use
to edit data (although not using an <EditItemTemplate>). When the use
clicks the Delete button, I want to prompt the user to confirm that the
want to delete the data before the postback occurs. The following line work
great for asp:Buttons that are not included in a DataList

btnDeleteWidget.Attributes.Add("onclick", "return confirm('Delete selecte
widget?');")

This line of code serves to pop up the message when the user clicks an
button being rendered as part of the DataList (in my case the sam
message/prompt appears when the user clicks either the Edit or the Delet
buttons)

MyDataList.Attributes.Add("onclick", "return confirm('Delete selecte
widget?');")

But what I want is for the message box/prompt to appear only when the use
clicks on the Delete button in the DataList (and to not appear when the use
clicks the Edit button that is also in the DataList). Any suggestions fo
how to accomplish this

Thanks
 
G

Guest

Jeff, looks like you're adding your snippet to anything that's clicked in the datalist (mydatalist, etc.). Instead, add it just to the delete button, at each ItemDataBound for the item rows. ht

Bil

----- Jeff S wrote: ----

I have a DataList that includes a <table> definition in the <ItemTemplate>
The <table> includes an asp:Button that, when clicked, causes a postbac
during which some data is deleted (it's a button labeled "Delete"). Th
<table> also includes a second button that, when clicked, enables the use
to edit data (although not using an <EditItemTemplate>). When the use
clicks the Delete button, I want to prompt the user to confirm that the
want to delete the data before the postback occurs. The following line work
great for asp:Buttons that are not included in a DataList

btnDeleteWidget.Attributes.Add("onclick", "return confirm('Delete selecte
widget?');")

This line of code serves to pop up the message when the user clicks an
button being rendered as part of the DataList (in my case the sam
message/prompt appears when the user clicks either the Edit or the Delet
buttons)

MyDataList.Attributes.Add("onclick", "return confirm('Delete selecte
widget?');")

But what I want is for the message box/prompt to appear only when the use
clicks on the Delete button in the DataList (and to not appear when the use
clicks the Edit button that is also in the DataList). Any suggestions fo
how to accomplish this

Thanks
 
J

Jeff S

Thanks Bill,

The ItemDataBound event was the missing piece of the puzzle for me.
Here's the code for anyone who might want to know what the solution looks
like:

private void MyDataList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e) {
Button curButton = (Button)e.Item.FindControl("btnDeleteWidget");
if (curButton != null) {
curButton.Attributes.Add("onclick", "return confirm('Delete widget?');");
}
}

-Jeff



Bill Borg said:
Jeff, looks like you're adding your snippet to anything that's clicked in
the datalist (mydatalist, etc.). Instead, add it just to the delete button,
at each ItemDataBound for the item rows. hth
 

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