从服务器调用 JQuery 函数 (asp.net)

Calling JQuery Function from Server (asp.net)

本文关键字:asp net 函数 服务器 调用 JQuery      更新时间:2023-09-26

在我使用 C# 的 asp.net 项目中,我需要从代码隐藏调用 JQuery 函数。以下是包含所需脚本的函数:

<link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
   function showFancy(url, title, width, height) {
        //alert(url);
        $(".fancyBox").fancybox({
            'width': width,
            'height': height,
            'autoScale': true,
            'transitionIn': 'elastic',
            'transitionOut': 'none',
            'type': 'iframe',
            'overlayColor': '#000000',
            'overlayOpacity': 0.7,
            'position': 'fixed',
            'scrolling': 'yes',
            'modal': false,
            'target': "_parent",
            "onClosed": function () {
                window.location = window.location;
            }
        });
        $('.fancyBox').attr({ href: url, title: title });
        $('.fancyBox').click();
    }

以及我从 aspx 调用的方式.cs:

           protected void Button1_Click(object sender, EventArgs e)
        {
ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", "showFancy('NewForm.aspx?ordernumber2=' + $('#TxtContractNumber').val(), 'Send', '55%', '65%');", true);
            }

当我使用输入按钮从客户端调用函数时,它工作正常,但是当我从服务器端调用它时,它不会调用 newform.aspx(当我从 showFancy 函数中取消注释"alert(url);"时,警报有效)。我相信我称之为"新形式"的方式有一个问题。提前感谢您的时间和回答。

如果你想

在点击按钮 Button1 时执行这个 javascript,你需要在代码隐藏中将其添加为 onclick 属性。您可以在 Page_LoadOnPreRender 上添加此属性。

Button1.Attributes.Add("onclick", "showFancy(...); return true;")

最后返回 true 将在 javascript 之后调用按钮的服务器端(Button1_Click),如果你需要做一些服务器操作。