繁体   English   中英

在C#asp.net中编写代码的Javascript

[英]Javascript to code behind in c# asp.net

嗨,我想找出答案后,我这样做:

C#

div.Attributes.Add("onclick", "return confirm_delete();");

JavaScript的:

function confirm_delete()
{
  if (confirm("Are you sure you want to delete this comment?")==true)
    return true;
  else
    return false;
}

如果我按“是”,它会弹出一个带有“是”和“否”的消息框,如何将函数添加到C#中,如何调用它?

如果是,请转到后面的C​​#代码

and add in confirm_delete 
if yes was clicked
do this 

我需要它,所以当有人单击“是”时,我可以(从asp.net c#中的后面代码中)添加一种从mysql数据库中删除的方法。

只是不知道我如何看待javascript

public static string confirm_delete()
{

    return something (dont know how to)

}

您的JavaScript将需要调用服务器上的页面。 通常,这是通过AJAX和Web服务完成的。

MSDN上的此页面很好地概述了所涉及的内容

如果您有兴趣使用jQuery,则此页面有一个简单的示例

您可能可以使用PageMethod来调用您的codebehind函数,请查看以下链接以获取示例

ASP.NET Ajax PageMethods

如何在客户端单击事件中添加按钮,而不是在后面的代码中添加按钮。 检查弹出窗口是否返回true。 如果返回false,则应防止触发服务器端事件。

<asp:Button ID="btn" OnClientClick="if(confirm_delete()){
/* post back*/
}else{
return false;
};" OnClick="btnDelete_Click" runat="server" Text="delete"/>



protected void btnDelete_Click(object sender, EventArgs e)
{

  //run your serverside code if confirm was pressed.

}

想知道为什么要使用div? 您不能使用asp.net Button / LinkBut​​ton并基于确认值限制它的回发吗? 例如

<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
    CommandName="Delete" Text="Delete"
    OnClientClick="return confirm('Are you certain you want to delete this 
product?');">
</asp:LinkButton>

参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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