admin管理员组文章数量:1321446
I'm attempting to call a javascript method in its own file from code behind on a button click.
aspx file
protected void Next_Click(object sender, EventArgs e)
{
if (hidden.Value == "")
{
Response.Write(@"<script language='javascript'>drawImage();</script>");
}
}
js file
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.
Any help you could give would be greatly appreciated.
I'm attempting to call a javascript method in its own file from code behind on a button click.
aspx file
protected void Next_Click(object sender, EventArgs e)
{
if (hidden.Value == "")
{
Response.Write(@"<script language='javascript'>drawImage();</script>");
}
}
js file
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.
Any help you could give would be greatly appreciated.
Share Improve this question asked May 7, 2013 at 3:57 TurtleTopiaryTurtleTopiary 1332 gold badges2 silver badges7 bronze badges3 Answers
Reset to default 1see http://msdn.microsoft./de-de/library/z9h4dk8y.aspx
try this
string jquery = "drawImage();"
ClientScript.RegisterStartupScript(typeof(Page), "a key",
"<script type=\"text/javascript\">"+ jquery +"</script>"
);
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:
ButtonName.Attributes.Add("onclick", {YourFunction});
in Page_Load
event.
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.
Hope it will help. My best, Alex
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) ..
protected void Next_Click(object sender, EventArgs e)
{
if (hidden.Value == "")
{
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), 'startupScript', 'drawImage();', true);
}
}
More Info from SO
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;
}
}
本文标签: Calling Javascript function from code behind ASPNETStack Overflow
版权声明:本文标题:Calling Javascript function from code behind ASP.NET - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742100175a2420766.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论