简体   繁体   中英

How to call server side code from javascript in asp.net?

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?

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.

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");
     }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM