【问题标题】:How to call javascript function from asp.net button click event如何从asp.net按钮单击事件调用javascript函数
【发布时间】:2011-06-01 14:54:57
【问题描述】:

如何从 asp.net 按钮单击事件中调用 showDialog。我的页面是一个内容页面,它有一个与之关联的母版页。

我已经尝试了以下

<asp:Button ID="ButtonAdd" runat="server" Text="Add" 
                            OnClientClick="showDialog('#addPerson');" />
  <asp:Button ID="ButtonAdd" runat="server" Text="Add" 
                            OnClientClick="showDialog(#<%=addPerson.ClientID %>);" />

我还必须从 gridview 模板按钮调用相同的函数来修改对话框上的记录。

<script type="text/javascript">


    // Used the following example to open the dialog withJquery 
        var dl;
        $(document).ready(function () {

            //Adding the event of opening the dialog from the asp.net add button.
            //setup edit person dialog             
            $('#addPerson').dialog({
                //Setting up the dialog properties.
                show: "blind",
                hide: "fold",
                resizable: false,
                modal: true,
                height: 400,
                width: 700,
                title: "Add New Member",
                open: function (type, data) {
                    $(this).parent().appendTo("form:first");
                }
            });

            //setup edit person dialog             
            $('#editPerson').dialog({
                //Setting up the dialog properties.
                show: "blind",
                hide: "fold",
                resizable: false,
                modal: true,
                height: 400,
                width: 700,
                title: "Modify Member",
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                }             
            });



            function showDialog(id) {
                $('#' + id).dialog("open"); 
            } 



    //        function closeDialog(id) {
    //            $('#' + id).dialog("close"); 
    //        } 

            //Adding a event handler for the close button that will close the dialog 
            $("a[id*=ButtonCloseDlg]").click(function (e) {
                $("#divDlg").dialog("close");
                return false;
            });
        });

       </script>

试图从 gridview 编辑按钮调用 jquery 对话框并得到相同的错误对象不支持此属性或方法?

<input type="submit" name="ctl00$ContentPlaceHolder1$GridViewMembers$ctl02$Button1" value="Edit" onclick="showDialog(&#39;addPerson&#39;);" id="ContentPlaceHolder1_GridViewMembers_Button1_0" />

【问题讨论】:

  • 你试过OnClientClick="showDialog('#&lt;%=addPerson.ClientID %&gt;');" 吗?

标签: javascript jquery asp.net content-pages


【解决方案1】:

如果您在按下此按钮时不需要启动回发,则不需要增加服务器控件的开销。

<input id="addButton" type="button" value="Add" />

<script type="text/javascript" language="javascript">
     $(document).ready(function()
     {
         $('#addButton').click(function() 
         { 
             showDialog('#addPerson'); 
         });
     });
</script>

如果您仍然需要回发,您可以使用稍微不同的代码有条件地停止其余按钮操作:

<asp:Button ID="buttonAdd" runat="server" Text="Add" />

<script type="text/javascript" language="javascript">
     $(document).ready(function()
     {
         $('#<%= buttonAdd.ClientID %>').click(function(e) 
         { 
             showDialog('#addPerson');

             if(/*Some Condition Is Not Met*/) 
                return false;
         });
     });
</script>

【讨论】:

  • $("#&lt;%=ButtonAdd.ClientID %&gt;").click(function () { var dl = $("#divDlg").dialog({ //Setting up the dialog properties. show: "blind", hide: "fold", resizable: false, modal: true, height: 400, width: 700 }); // dl.parent().appendTo(jQuery('form:first')); }); 这就是我让添加按钮工作的方式。因为这个问题越来越乱,所以要为 gridview 按钮开始一个新问题。
  • 答案中的第二个例子对我来说是开箱即用的,很好的 asp foo,谢谢。
【解决方案2】:

您已经在 showDialog() 函数中添加了井号,并且在第二个代码 sn-p 中缺少单引号。您还应该从处理程序返回false 以防止发生回发。试试:

<asp:Button ID="ButtonAdd" runat="server" Text="Add"
    OnClientClick="showDialog('<%=addPerson.ClientID %>'); return false;" />

【讨论】:

  • @Spafa9,看起来像是范围问题。尝试在 document.ready() 块之外声明 showDialog() 函数。
  • 我们是否必须将 js 字符串硬编码到 ASP 控件中?我们不能使用不显眼的 JavaScript 吗?
  • @Raynos,你的意思是在客户端注册一个click 处理程序而不是使用ClientClick 事件?它可以说是更好的设计,但我不认为提问者在问这个问题:)
  • 这让我想起了&lt;div onclick="doSomeMagic();"&gt;&lt;a href="javascript: void HackYou();"&gt;,让我畏缩不前。但务实就好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 2023-03-15
  • 2014-02-09
  • 1970-01-01
  • 2011-11-30
  • 2010-10-07
相关资源
最近更新 更多