Archive for August, 2008

Asp.Net repeater control, delete with confirmation

August 29, 2008

Asp.Net Repeater Control: Confirmation

Maybe I missed something, but I couldn’t immediately figure out how to relate the
many examples
I found for putting some confirmation javascript on a delete button in
an Asp.Net GridView related to the repeater control I wanted to use.

 

I ended up getting it to work by calling doPostBack and using the hidden values that Asp.Net sets for you
when you do a form postback.

Here’s what it looks like:

 

The javascript looks like this:

// -------------------------------
// how to use: "javascript:HandleDeleteWithConfirmation(this,'Delete$');"
function HandleDeleteWithConfirmation(ctl , cmd )
{
    var iRet = confirm('Are you sure you want to delete?');
    if ( iRet == 0 ) return;
    
    __doPostBack(ctl.id,cmd);
}
// -------------------------------

The html looks like this. The syntax for the plain html image was much easier to get to work
than the image link server control.



 <img 
id="imgBtnDelete<%# Eval("AccountKey")%>" 
src="images/ButtonBarDelete.gif" 
onmouseover="this.src='images/ButtonBarDeleteOver.gif';" 
onmouseout="this.src='images/ButtonBarDelete.gif';" 
onclick="javascript:HandleDeleteWithConfirmation(this,'Delete$<%# Eval("AccountKey")%>');" 
style="border:none" 
alt = "Delete account" 
 />
 
<asp:ImageButton ID="btnDelete" 
ImageUrl="images/ButtonBarDelete.gif" 
onmouseover="this.src='images/ButtonBarDeleteOver.gif';"
onmouseout="this.src='images/ButtonBarDelete.gif';"
CommandArgument='<%# Eval("AccountKey") %>' 
OnClientClick='<%# "HandleDeleteWithConfirmation(this, " & chr(39) & "Delete$" & Eval("AccountKey") & chr(39) & ");" %>'
CommandName="Delete" 
 CausesValidation="true" 
runat="server" 
tooltip="Delete account" 
/>
<!-- 
you can't have a single quote, you'll get a bad formed server control
OnClientClick='<%# "test(this, " & chr(39) & "Delete$" & Eval("AccountKey") & chr(39) & ");" %>'
-->

And on the server side, what I do is look at the control id, see if it was my delete button, and if so,
get the record id out of the parameter passed down, and then delete it.

        If (IsPostBack) Then
            ' grap the two asp.net default hidden form values that the postback sets
            ' the first one is the source of the event, the second one is a parameter
            sCmd = Request.Form("__EVENTTARGET")
            sEvent = Request.Form("__EVENTARGUMENT")
            Diagnostics.Debug.Print(sCmd & " " & sEvent)

            If (sCmd.ToLower().IndexOf("btndelete") >= 0) Then
                handle_delete(sEvent) ' sEvent looks like "Delete$13" - delete record 13
            End If