asp.net按钮单击客户端和服务器端代码

时间:2022-02-20 03:50:18

I have an asp:button called "Delete" and what I want to do is have a JavaScript confirm popup with the options "Yes" and "No". If "Yes" then delete a record from a SQL DB else if "No" then nothing should happen.

我有一个名为“删除”的asp:按钮,我想要做的是有一个JavaScript确认弹出窗口,其中包含选项“是”和“否”。如果“是”则从SQL DB中删除记录,否则如果“否”则不会发生任何事情。

<asp:Button runat="server" ID="btnDelete" Text="Delete" 
  OnClientClick="if(confirm('Delete?'))alert('You chose yes!');else alert('You chose no!')" 
  OnClick="btnDelete_Click" />

btnDelete_Click contains SQL delete logic.

btnDelete_Click包含SQL删除逻辑。

The problem I am having is that the OnClick method always gets executed. Whether you pick "Yes" or "No" from the JavaScript popup the record gets deleted regardless. It seems to always cause a postback.

我遇到的问题是OnClick方法总是被执行。无论您从JavaScript弹出窗口中选择“是”还是“否”,记录都会被删除。它似乎总是会引发回发。

Can I somehow get the "Yes" or "No" result into my code behind so I can actually do a simple "if" statement for the delete logic?

我可以以某种方式在我的代码中得到“是”或“否”结果,所以我实际上可以为删除逻辑做一个简单的“if”语句吗?

4 个解决方案

#1


5  

Try this, on your javascript do this

试试这个,在你的javascript上做这个

var result = Confirm("Are you sure . . . . ");
if(result)
   return true;
else
   return false;

what it does is if it's true, it should postback your code, else it'd cancel your click.

它的作用是,如果它是真的,它应该回发你的代码,否则它会取消你的点击。

#2


0  

That should be:

那应该是:

<asp:Button runat="server" ID="btnDeleteSite" Text="Delete" OnClientClick="return confirm('Format Delete?');" OnClick="btnDeleteSite_Click" />

#3


0  

You should ideally put JS event handlers in the script tag, and call them from your OnClientClick event in the button definition. Like so:

理想情况下,您应该在脚本标记中放置JS事件处理程序,并从按钮定义中的OnClientClick事件中调用它们。像这样:

<script type="text/javascript">
    function ConfirmDelete() {
            return confirm("Delete?");
        }
</script>

And then in your button's OnClientClick event, you do this:

然后在按钮的OnClientClick事件中,执行以下操作:

OnClientClick="return ConfirmDelete()"

This keeps your markup clean and readable, and will also prevent the postback from happening if the user chooses 'No'.

这样可以保持标记的清晰和可读性,并且如果用户选择“否”,也会阻止回发发生。

#4


0  

Try with this

试试这个

<asp:Button runat="server" ID="btnDelete" Text="Delete" OnClientClick="if(confirm('Delete?')){return true;}else {return false;} OnClick="btnDelete_Click" />

When you are using OnClientClick event with JS confirm dialog box at that time you need to return true or false. If it will return True then OnClick(Server side) event will fire otherwise it will not.

当您使用带有JS确认对话框的OnClientClick事件时,您需要返回true或false。如果它将返回True,则OnClick(服务器端)事件将触发,否则不会。

#1


5  

Try this, on your javascript do this

试试这个,在你的javascript上做这个

var result = Confirm("Are you sure . . . . ");
if(result)
   return true;
else
   return false;

what it does is if it's true, it should postback your code, else it'd cancel your click.

它的作用是,如果它是真的,它应该回发你的代码,否则它会取消你的点击。

#2


0  

That should be:

那应该是:

<asp:Button runat="server" ID="btnDeleteSite" Text="Delete" OnClientClick="return confirm('Format Delete?');" OnClick="btnDeleteSite_Click" />

#3


0  

You should ideally put JS event handlers in the script tag, and call them from your OnClientClick event in the button definition. Like so:

理想情况下,您应该在脚本标记中放置JS事件处理程序,并从按钮定义中的OnClientClick事件中调用它们。像这样:

<script type="text/javascript">
    function ConfirmDelete() {
            return confirm("Delete?");
        }
</script>

And then in your button's OnClientClick event, you do this:

然后在按钮的OnClientClick事件中,执行以下操作:

OnClientClick="return ConfirmDelete()"

This keeps your markup clean and readable, and will also prevent the postback from happening if the user chooses 'No'.

这样可以保持标记的清晰和可读性,并且如果用户选择“否”,也会阻止回发发生。

#4


0  

Try with this

试试这个

<asp:Button runat="server" ID="btnDelete" Text="Delete" OnClientClick="if(confirm('Delete?')){return true;}else {return false;} OnClick="btnDelete_Click" />

When you are using OnClientClick event with JS confirm dialog box at that time you need to return true or false. If it will return True then OnClick(Server side) event will fire otherwise it will not.

当您使用带有JS确认对话框的OnClientClick事件时,您需要返回true或false。如果它将返回True,则OnClick(服务器端)事件将触发,否则不会。