简体   繁体   English

JavaScript函数阻止执行C#函数?

[英]JavaScript functions prevent C# functions from being executed?

On my aspx page I have two buttons. 在我的aspx页面上,我有两个按钮。 One calls some javascript and the other calls a C# function on the code-page. 一个调用一些javascript,另一个调用代码页上的C#函数。 When I call the javascript file in the head of the document, the JavaScript function works well but the C# does not. 当我在文档的头部调用javascript文件时,JavaScript函数运行良好,但C#没有。 Clicking on the button does not do anything. 单击按钮不会执行任何操作。 If I remove the javascript call then the C# function works normally. 如果我删除了javascript调用,那么C#函数正常工作。

How can I overcome this? 我怎么能克服这个? It seems as if it is expecting to find the C# function within the JavaScript file. 似乎期望在JavaScript文件中找到C#函数。

ASP: ASP:

<head>
<script src="MyFunctions.js" language="javascript" type="text/javascript"></script>
</head>
<body>
<asp:Button id="btn1" Text="Submit" runat="server" OnClick="buttonSumbit_Click" />
<asp:Button id="btn2" Text="Show" runat="server" OnClientClick="buttonShow_Click()" />
</body>

C#: C#:

protected void buttonSumbit_Click(object sender, EventArgs e)
{
//SOME CODE HERE
}

JavaScript: JavaScript的:

function buttonShow_Click()
{
//SOME CODE HERE
}

您需要将所有Asp.Net服务器控件放在<form runat="server"></form>标记内

You need to modify your javascript function call to be like this 你需要修改你的javascript函数调用就像这样

<asp:Button id="btn2" Text="Show" runat="server" OnClientClick="return buttonShow_Click()" />

and make sure that your javascript function returns true so postback (execute C# function) happens. 并确保您的javascript函数返回true,以便发生回发(执行C#函数)。 If your javascript function return false, postback to server won't happen. 如果您的javascript函数返回false,则不会回发到服务器。

I Suggest you to use this for calling javascript functions: 我建议你使用它来调用javascript函数:

OnClientClick="if(!hidepopup())return false;"

you can also call javascript functions from codebehind: 你也可以从codebehind调用javascript函数:

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "jsfunction();", true);

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

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