我有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中调用repeater delete命令逻辑? 有没有其他方法可以做到这一点?
答案 0 :(得分: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,是获取按钮及其回发之间的唯一方法。
答案 1 :(得分:1)
您可以使用repeater的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>
<强> 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");
}
}