C#代码背后的确认框 [英] Confirmation box in C# code behind

查看:92
本文介绍了C#代码背后的确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在asp.net项目的C#代码中动态生成了按钮。我为每个动态生成的按钮编写了一个事件处理程序。我需要从后面的代码中提示确认消息,因为它是一个删除操作。



我尝试过:



ScriptManager.RegisterStartupScript(this,this.GetType(),Message,confirm('你想删除吗?');,true);





如何获得返回值,用户选择ok还是取消?

解决方案

你所展示的不是来自背后代码的确认,而是javascript。你只是在服务器上写它,但它在客户端上执行。



创建一个将在客户端按钮点击时调用的函数(有一个属性Aspx Button控件OnClientClick。在该函数中写下如下内容:



返回确认(你确定要删除吗?); 





这样,Yes将返回true并且你将获得代码隐藏事件。否则将返回false并且服务器将返回(并且事件将没有发生)。



这样做的好处是你可以在页面上或文件中写入函数,而不需要注册脚本,只是将ClientClick属性设置为return functionName();


要求用户确认他们想要继续操作| ASP.NET论坛 [

I have generated buttons dynamically in the C# code behind for an asp.net project. I have written a event handler for each dynamically generated button. I need prompt a confirmation message from the code behind as it is a delete operation.

What I have tried:

ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "confirm('Do you Want to Delete ? ');", true);


how to get the return value, the user's choice either "ok" or "cancel" ?

解决方案

What you're showing is not a confirmation from the code behind, but javascript. You're just writing it on the server, but it executes on the client.

Make a function that will be called on client button click (there is a property on Aspx Button control OnClientClick. In that function write something like:

return confirm("Are you sure you want to delete?");



That way, Yes will return true and you'll get code behind event. No will return false and the server trip (and event will not happen).

The advantage of this is that you can have the function written on the page or in a file somewhere and you don't need to register the script, just to set ClientClick property to "return functionName();"


Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]


这篇关于C#代码背后的确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆