如何从asp.net内容页面调用javascript函数 [英] How to Call javascript function from asp.net content page

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

问题描述

我试图从后面的代码调用javascript函数

这是我的javascript函数

i was trying to call a javascript function from a back code
this is my javascript function

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
        function myFunction() 
			{
                alert("hello");
			}
</script>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<asp:Button ID="btnSearch" Text="Go" runat="server" OnClick="btnSearch_Click" />







i尝试在后面的代码上调用这样的函数但没有工作.....




i tried calling the function like this on back code but not working.....

protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "myFunction()", true);
        }

推荐答案

我尝试了我的测试应用程序的示例,它正常工作,因为您也可以尝试由我:



I tried your example with my testing application and it's working properly as you can also try the code given by me:

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">

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

</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="ContentPlaceHolder2">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>





即使在页面背面的代码上,也可以在按钮点击时编写调用函数代码:





And write a code for invoke function on button click even on code behind page:

protected void Button1_Click(object sender, EventArgs e)
       {
           ScriptManager.RegisterStartupScript(Page, Page.GetType(), "GetMSG", "GetMSG();", true);
           //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "GetMSG()", true);
       }





请尝试这个例子。我希望它对你有所帮助.....谢谢......: - )



please try this example. I hope its helping to you.....Thank You......:-)


首先我建议你在.js文件中编写你的JavaScript并添加引用您的aspx页面。

First of all I would suggest you to write your JavaScript in .js file and add reference to your aspx page.
<script src="../JS/MyJavascript.js" type="text/javascript"></script>



然后尝试调用这样的JavaScript函数:


Then try calling your JavaScript function like this:

ScriptManager.RegisterClientScriptBlock(base.Page, this.GetType(), "MyScript", "myFunction();", true);





- Amy


如果你想在单击按钮时调用它,你可以在客户端,befreo回发,通过添加page_load

If you want to call it when the button is clicked you can do so on the client side, befreo postback, by adding in page_load
btnSearch.Attributes.Add("onlcick", "myFunction()")



您甚至可以通过返回false函数来阻止回发。

但是,如果你真的想在代码回发后从click事件中调用它:


You can even then prevent the postback by returning false form your JavaScript function.
However, if you really want to call it from the click event after postback in code:

ClientScript.RegisterStartupScript(Me.GetType(), "alert", "myFunction()", True)


这篇关于如何从asp.net内容页面调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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