0

我有Repeater control这样的;

<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>

我的页面后面的代码如下所示;

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

我在 .aspx 文件中的 javascript 代码如下所示:

<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>

如何在我的 javascrpt 中调用中继器删除命令逻辑?有没有其他方法可以做到这一点?

4

2 回答 2

1

在按钮单击中使用 OnClientClick 并在其中写入函数名称。当函数返回 false 时,则不调用服务器端方法。如果 JS 函数返回 true,则执行服务器端方法

<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,是在按钮和它的回发之间获取的唯一方法。

于 2012-08-09T09:31:27.477 回答
1

您可以使用中继器的 ItemDatabound 属性将 Javascript 函数绑定到 Dalete Button Onclick 事件,如下所示...

代码隐藏:-

 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> 

删除页面.aspx:

function pageLoad(sender, eventArgs) 
{ 
    if(!IsPostBack)
     {
       int IdToBeDeleted=Request.QueryString["id"];
       Write Your Delete Code Here...
       if delete successful...Response.Write("Delete Successful");
     }
} 
于 2012-08-09T09:36:44.317 回答