Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have Repeater control like this;

<asp:Repeater ID="repeaterCategoryList" runat="server" 
                onitemcommand="repeaterCategoryList_ItemCommand">
                <ItemTemplate>
                        <td class="center">
                            <asp:Button ID="buttonDelete" runat="server" CssClass="btn btn-primary" CommandName="Delete" Text="Delete" 
                                CommandArgument='<%# Eval("CategoryId") %>'/>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>

And my code behind page looks like this;

protected void repeaterCategoryList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
         //my server side logic here
    }
}

And my javascript code in .aspx file looks like this:

<script>
    $(function () {
        $('#buttonDelete').live('click', function (e) {
            e.preventDefault();
            $.alert({
                type: 'confirm'
                , title: 'Alert'
                , text: '<p>Are you sure, you want to delete this category</p>'
                , callback: function () {

                    // call server side here
                }
            });
        });

    });
</script>

How do I call the repeater delete command logic inside my javascrpt? Is there any alternative way to do this?

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

up vote 1 down vote accepted

You can use ItemDatabound property of repeater to bind the Javascript function to Dalete Button Onclick event as follow...

CodeBehind:-

 void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)  
 {     
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      {
        int IdToBeDeleted=((Label)e.Item.FindControl("idFieldControl")).Text;     
        Button Btn= (Button)e.Item.FindControl("buttonDelete");     
        Btn.Attributes.Add("onclick","return ConfirmDelete('"+IdToBeDeleted+"')");    
      }

 } 

Javascript:

<script>  
  function ConfirmDelete(var idVal) 
  {    
     if(Confirm("Are you sure, you want to delete this category?")) 
      {
         var xmlhttp;
         if (window.XMLHttpRequest)
         {// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp=new XMLHttpRequest();
         }
         else
         {// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }


         xmlhttp.onreadystatechange=function()
         {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
               {
                 alert(xmlhttp.responseText);
               }
         }
          xmlhttp.open("POST","DeletePage.aspx?id="+idVal,true);  
          xmlhttp.Send();
      }
  } 
</script> 

DeletePage.aspx:

function pageLoad(sender, eventArgs) 
{ 
    if(!IsPostBack)
     {
       int IdToBeDeleted=Request.QueryString["id"];
       Write Your Delete Code Here...
       if delete successful...Response.Write("Delete Successful");
     }
} 
share|improve this answer
how do I execute c# method inside ConfirmDelete() – DotNet Dreamer Aug 9 '12 at 9:58
You cannot execute c# method in Javascript Function...if you want so you can use Ajax to do so...Call Ajax method from inside the javascript function ... – Amol Kolekar Aug 9 '12 at 10:14
Please have a look at edited Code... – Amol Kolekar Aug 9 '12 at 10:30
add comment (requires an account with 50 reputation)

Use OnClientClick in the button click and write the function name in it. when the function returns false, then the server side method is not called. If the JS Function returns true, then the server side method is executed

<asp:Button ID="buttonDelete" runat="server" CssClass="btn btn-primary" CommandName="Delete" Text="Delete" onclick="Button_Click_Event" OnClientClick="return Javascript_Function()" CommandArgument='<%# Eval("CategoryId") %>'/>

OnClientClick, is the only way to get between a button and its post back.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.