Add Confirm to Delete button in repeater

R

Roger Twomey

I have a repeater which shows Group names.

(Skip this part if you want rather irrelevant)
The groups are ordered and formatted:
Parent
---Child
------Grand Child

(okay start reading again please)

The code (html side) looks like this:



<table id="Table1">

<asp:Repeater id="rptGroups" runat="server"
OnItemCommand="Button_ItemCommand">

<HeaderTemplate>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td>

<img width='<%# Int32.parse(DataBinder.Eval(Container.DataItem, "lvl")
* 20) %>' height="1" />

<asp:Label Runat="server" CssClass="FormFieldLabel">

<%# DataBinder.Eval(Container.DataItem, "GroupName") %>

</asp:Label>

</td>

<td>

<asp:LinkButton CommandName="ID"
Text='<%#DataBinder.GetPropertyValue(me, "SelectUserButtonName")%>'
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "GrpID")%>'
runat="server" ID="Linkbutton1" CssClass="FormLnkBtn" />

</td>

</tr>

</ItemTemplate>

<FooterTemplate>

</FooterTemplate>

</asp:Repeater>

</table>



the link button line:


<asp:LinkButton CommandName="ID" Text='<%#DataBinder.GetPropertyValue(me,
"SelectUserButtonName")%>'
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "GrpID")%>'
runat="server" ID="Linkbutton1" CssClass="FormLnkBtn" />



Creates a link button, sets the text = the value of "SelectUserButtonName"
property of the parent page (this phrase is dynamically changed depending on
the language of the user).

It adds a CommandArgument which happens to be the key field value for that
group. Currently when a user clicks the button the code collects the value
from the CommandArguement to know which row to work with.



Sub Button_ItemCommand(ByVal Sender As Object, _

ByVal e As RepeaterCommandEventArgs)

If e.CommandName = "ID" Then

'PopUp(e.CommandArgument.ToString() + " " + hdnFor.Value)

RaiseEvent UserSelected(e.CommandArgument.ToString)

End If

End Sub


I want to dynamically add code to confirm a delete.

I have done this before with regular buttons and link buttons however, as
this one seems to be dynamically added I can't refer to it by ID because I
get an error saying it doesn't exist.

Here is how the HTML renders right now:



<a id="ShowGroupMembership1_rptGroups__ctl6_Linkbutton1" class="FormLnkBtn"
href="javascript:{if (typeof(Page_ClientValidate) != 'function' ||
Page_ClientValidate())
__doPostBack('ShowGroupMembership1$rptGroups$_ctl6$Linkbutton1','')}
">Delete</a>



as you can see the id is NOT LinkButton1 anymore, so of course I cannot find
it.

Just to make things a bit more difficult, this is a control and depending on
parameters set it can either delete the group OR simply Raise and event to
the parent sending the key field value of the selected group.

I am currently trying to get the confirm delete to work.

Thanks for your time!

Oh, VB please..
I did find this link:
http://davidhayden.com/blog/dave/archive/2004/03/16/178.aspx
but I haven't figured out how to convert from C#
 
M

Morgan

Roger,
I haven't worked with the repeater command enough to offer a repeater
specific solution, but here is a snippet I use for the datagrid
ItemDataBound command...
The trick to this is knowing which table cell index contains, in this case,
the image button used for the delete action on the button. Works like a
charm for the datagrid.

--Morgan

Private Sub grdInvoiceLines_ItemDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
grdInvoiceLines.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim myTableCell As TableCell
Dim myEditCell As TableCell
myTableCell = e.Item.Cells(6)
myEditCell = e.Item.Cells(5)
Dim myDeleteButton As ImageButton
myDeleteButton = CType(myTableCell.Controls(1),
System.Web.UI.WebControls.ImageButton)
myDeleteButton.Attributes.Add("onclick", "return
confirm('Are you Sure you want to delete this Line Item?');")
End Select
End Sub
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top