Calling Javascript function from code behind ASP.NET

后端 未结 3 892
失恋的感觉
失恋的感觉 2021-01-27 04:31

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, EventA         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-27 05:01

    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.

    
    

    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;
        }
    }
    

提交回复
热议问题