简体   繁体   English

如何从asp.net中的代码隐藏调用javascript函数

[英]how to call a javascript function from codebehind in asp.net

protected void addSchoolButtonClick(object sender, ImageClickEventArgs e)
    {

        Page.ClientScript.RegisterStartupScript(GetType(), "MyKey1", "SchoolSearchPopUp();", true);

        /*Some code*/
    }

I am developing a website in asp.net.At a Hyperlink onclick event i want to call a javascript function"SchoolSearchPopUp()" .this function is for creating a new popup window and it is working correctly.But my problem is ,a javascript function is calling or pop window opens only after executing the rest of the code in that function and that code need's some data that occurs as a result of popup.How can i create the popup before executing the rest of code in that function. 我正在asp.net中开发一个网站。在超链接onclick事件中,我想调用一个javascript function"SchoolSearchPopUp()"函数用于创建一个新的弹出窗口,并且它可以正常工作。但是我的问题是,一个javascript该函数正在调用或弹出窗口仅在执行该函数中的其余代码之后才打开,并且该代码需要一些由于popup发生的数据。我如何在执行该函数中的其余代码之前创建弹出窗口。

将回发触发器更改为弹出窗口中的内容。

I don't think javascript can be called from code behind. 我不认为可以从后面的代码中调用javascript。 C# is running from the server and java is all client side. C#正在服务器上运行,而java是所有客户端。 There's a good explanation to a similar question here: http://forums.asp.net/t/1117189.aspx 这里有一个类似问题的很好的解释: http : //forums.asp.net/t/1117189.aspx

If you need to execute a javascript function, you could try changing the hyperlink to a button and making use of the OnClientClick property. 如果需要执行javascript函数,则可以尝试将超链接更改为按钮并利用OnClientClick属性。 This executes script client side rather than calling a method on the server. 这将执行脚本客户端,而不是在服务器上调用方法。

     <asp:button id="Button1"
                 text="Click Here"
                 onclientclick="SchoolSearchPopUp()"
                 runat="server" />

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.button.onclientclick.aspx

You will need to write JavaScript on the page to handle the click of the button first and then call to the page method on the server. 您将需要在页面上编写JavaScript,以首先处理按钮的单击,然后在服务器上调用page方法。 Add an OnClientClick attribute to your button element and run your JavaScript method from there: 向按钮元素添加OnClientClick属性,然后从此处运行JavaScript方法:

<asp:Button ID="TestButton" OnClientClick="SchoolSearchPopup()" Text="Click Me" OnClick="addSchoolButtonClick" runat="server"/>

<script type="text/javascript">
    function SchoolSearchPopup()
    {
        alert("Popup");
    }
</script>

If you want to execute some javascript before your postback you will need to register your hyperlink's click event to a js method, then submit your post to the server after performing whatever client side logic you are looking to run. 如果要在回发之前执行一些JavaScript,则需要将超链接的click事件注册到js方法,然后在执行要运行的任何客户端逻辑后,将您的帖子提交到服务器。 (not the other way around, using RegisterStartupScript) (相反,使用RegisterStartupScript)

Example: 例:

$("#myHyperLink").click(function() {

    // do page logic, in your case show a modal window
    $("#myModalDivContainer").show();


    // submit your post to the server... replace targetClientID with ID of server control you're posting to
    __doPostBack('targetClientID', '');

   // NOTE: If you want to perform an AJAX request instead simply use some jQuery here instead. it's up to you how to handle the request from this point :)

});

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM