如何调用从asp.net按钮点击事件JavaScript函数 [英] How to call javascript function from asp.net button click event

查看:231
本文介绍了如何调用从asp.net按钮点击事件JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何调用ShowDialog的从asp.net按钮单击事件。我的网页是具有与其相关联的母版一contentpage。

我曾尝试以下

 < ASP:按钮的ID =ButtonAdd=服务器文本=添加
                            的OnClientClick =的ShowDialog('#addPerson的'); />
  < ASP:按钮的ID =ButtonAdd=服务器文本=添加
                            的OnClientClick =的ShowDialog(#&下;%= addPerson.ClientID%GT); />

我也将不得不从一个gridview模板按钮调用该函数相同修改对话框上的记录。

 <脚本类型=文/ JavaScript的>
    //使用以下示例打开对话框withJquery
        变种DL;
        $(文件)。就绪(函数(){            //添加从asp.net添加按钮打开对话框的事件。
            //设置编辑对话框人
            $('#addPerson的')。对话框({
                //设置对话框属性。
                显示:瞎子,
                隐藏:褶皱,
                可调整大小:假的,
                模式:真实,
                高度:400,
                宽度:700,
                标题:添加新成员
                开:功能(类型,数据){
                    $(本).parent()appendTo(形式:一是)。
                }
            });            //设置编辑对话框人
            $('#editPerson')。对话框({
                //设置对话框属性。
                显示:瞎子,
                隐藏:褶皱,
                可调整大小:假的,
                模式:真实,
                高度:400,
                宽度:700,
                标题:修改成员
                开:功能(类型,数据){
                    $(本).parent()appendTo(形式)。
                }
            });            功能的ShowDialog(ID){
                $('#'+ id)的.dialog(开放);
            }    //函数closeDialog(ID){
    // $('#'+ id)的.dialog(亲密);
    //}            //添加事件处理程序的关闭按钮,将关闭对话框
            $(一[ID * = ButtonCloseDlg])。点击(函数(五){
                $(#divDlg)对话框(关闭)。
                返回false;
            });
        });       < / SCRIPT>

试图从一个GridView editbutton调用jQuery的对话框,并得到同样的错误对象犯规支持此属性或方法?

 <输入类型=提交名称=ctl00 $ ContentPlaceHolder1 $ GridViewMembers $ ctl02 $ Button1的价值=编辑的onclick =的ShowDialog(安培;#39; addPerson的&安培; #39;); ID =ContentPlaceHolder1_GridViewMembers_Button1_0/>


解决方案

如果您不需要启动后回来时,你preSS这个按钮,然后又做了服务器控件的开销并不necesary。

 <输入ID =Add按钮类型=按钮值=添加/><脚本类型=文/ JavaScript的LANGUAGE =JavaScript的>
     $(文件)。就绪(函数()
     {
         $('#Add按钮')。点击(函数()
         {
             的ShowDialog('#addPerson的');
         });
     });
< / SCRIPT>

如果您仍然需要能够做回发,您可以有条件地一点点不同的code停止按钮动作的其余部分:

 < ASP:按钮的ID =buttonAdd=服务器文本=添加/><脚本类型=文/ JavaScript的LANGUAGE =JavaScript的>
     $(文件)。就绪(函数()
     {
         $('#<%= buttonAdd.ClientID%GT;')。点击(函数(五)
         {
             的ShowDialog('#addPerson的');             如果(/ *一些条件不符合* /)
                返回false;
         });
     });
< / SCRIPT>

How do I call the showDialog from a asp.net button click event. My page is a contentpage that has a masterpage associated with it.

I have tried the following

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

I am also going to have to call this same function from a gridview template button to modify the record on the dialog.

<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>

Tried to call the jquery dialog from a gridview editbutton and get the same error Object doesnt support this property or method?

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

解决方案

If you don't need to initiate a post back when you press this button, then making the overhead of a server control isn't necesary.

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

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

If you still need to be able to do a post back, you can conditionally stop the rest of the button actions with a little different code:

<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>

这篇关于如何调用从asp.net按钮点击事件JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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