从 ASP.NET 后面的代码调用 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16410968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Javascript function from code behind ASP.NET
提问by TurtleTopiary
I'm attempting to call a javascript method in its own file from code behind on a button click.
我试图在单击按钮时从后面的代码调用它自己的文件中的 javascript 方法。
aspx file
aspx文件
protected void Next_Click(object sender, EventArgs e)
{
if (hidden.Value == "")
{
Response.Write(@"<script language='javascript'>drawImage();</script>");
}
}
js file
js文件
function drawImage() {
context.drawImage(video, 0, 0, 320, 240);
var imgBase = canvas.toDataURL('image/jpeg');
document.getElementById("hidden").value = imgBase;
}
The problem is that this wont call the draw image method, i suspect it's cause the js file is its own file but i'm not sure.
问题是这不会调用绘制图像方法,我怀疑这是因为 js 文件是它自己的文件,但我不确定。
Any help you could give would be greatly appreciated.
您能提供的任何帮助将不胜感激。
采纳答案by Mahmoude Elghandour
see http://msdn.microsoft.com/de-de/library/z9h4dk8y.aspx
见http://msdn.microsoft.com/de-de/library/z9h4dk8y.aspx
try this
试试这个
string jquery = "drawImage();"
ClientScript.RegisterStartupScript(typeof(Page), "a key",
"<script type=\"text/javascript\">"+ jquery +"</script>"
);
回答by Sen Jacob
I presume you already included the script file in your page.
我假设您已经在页面中包含了脚本文件。
If you require the postback, you need ClientScriptManager.RegisterStartupScript Method (Type, String, String, Boolean)..
如果需要回发,则需要ClientScriptManager.RegisterStartupScript Method (Type, String, String, Boolean)..
protected void Next_Click(object sender, EventArgs e)
{
if (hidden.Value == "")
{
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), 'startupScript', 'drawImage();', true);
}
}
You can call the function from your button itself, without the postback.
您可以从按钮本身调用该函数,而无需回发。
<asp:Button runat="server" ID='next' OnClientClick="return drawImage();" />
and in the script
并在脚本中
function drawImage() {
if(document.getElementById("hidden").value != undefined || document.getElementById("hidden").value != "")
{
context.drawImage(video, 0, 0, 320, 240);
var imgBase = canvas.toDataURL('image/jpeg');
document.getElementById("hidden").value = imgBase;
return false;
}
}
回答by Alexander Bell
1). If the function is stored in external file (e.g. MyScript.js
) then you should include it in the <head>
section of the web page and add onclick
event to the button, like:
1)。如果该函数存储在外部文件中(例如MyScript.js
),那么您应该将其包含在<head>
网页的部分中onclick
并向按钮添加事件,例如:
ButtonName.Attributes.Add("onclick", {YourFunction});
in Page_Load
event.
在Page_Load
事件中。
2) Another approach is to assign the entire javascript function to a string variable and then pass it instead of {YourFunction} prefixed with javascript:
. In this case you don't even need the external file.
2) 另一种方法是将整个 javascript 函数分配给一个字符串变量,然后传递它而不是带有前缀的 {YourFunction} javascript:
。在这种情况下,您甚至不需要外部文件。
Hope it will help. My best, Alex
希望它会有所帮助。我最好的,亚历克斯