【问题标题】:Confirm dialog and how to control it in code behind确认对话框以及如何在后面的代码中控制它
【发布时间】:2013-05-28 21:53:09
【问题描述】:

当我加载页面时,我会加载这个脚本:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                              "question",
                              "<script type = 'text/javascript'>if(confirm('Are you sure you want to do this?')) return true; else return false;</script>",
                              false);

当用户点击yes在后面的代码中做某事,当点击no做其他事情时,我不知道如何处理?
如何处理后面代码中的这种点击?

【问题讨论】:

    标签: c# javascript asp.net code-behind scriptmanager


    【解决方案1】:

    如果你返回真/假,那么在你后面的代码中你只会得到真。 要获取 false 的值,您还必须创建另一个函数。

    我喜欢的最好的方法是在 hiddenfield 中设置返回参数的值。并在代码隐藏中获取此变量并相应地工作。

    【讨论】:

      【解决方案2】:

      您已经有了相应的代码。 您只能在是或否时调用 javascript 函数。但是 Javascript 函数可以使用 Ajax 调用 codebehind webmethods。

      ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                                    "question",
                                    "<script type = 'text/javascript'>if(confirm('Are you sure you want to do this?')){[Call Function for Yes]} else{ [Call Function for No or Cancel];}</script>",
                                    false);
      

      【讨论】:

        【解决方案3】:

        除了 WebMethods 技术之外,您还可以选择使用 __doPostBack 方法从您的 javascript 代码中命中代码隐藏。在http://aspalliance.com/895_Understanding_the_JavaScript___doPostBack_Function.allhttp://dopostback.net/了解更多关于它的使用信息。

        使用 WebMethods 的一个限制是,由于它们始终是静态的,因此您的非静态字段、方法和控件将无法在其中访问。 http://forums.asp.net/t/1531113.aspx/1.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多