C# 在C中调用Javascript#

C# 在C中调用Javascript#,c#,javascript,C#,Javascript,如何在c#linkbutton_click事件中调用以下javascript。 下面的代码在aspx页面上,我想从codebehind调用它 <script type="text/javascript"> function PrintPanel() { var panel = document.getElementById("<%=pnlContents.ClientID %>"); var printWindow = window

如何在c#linkbutton_click事件中调用以下javascript。 下面的代码在aspx页面上,我想从codebehind调用它

<script type="text/javascript">
    function PrintPanel() {
        var panel = document.getElementById("<%=pnlContents.ClientID %>");
        var printWindow = window.open('', '', 'height=1000,width=900');
        printWindow.document.write('<html><head>');
        printWindow.document.write('</head><body >');
        printWindow.document.write(panel.innerHTML);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        setTimeout(function () {
            printWindow.print();

        }, 500);
        return false;
    }
</script>

函数PrintPanel(){
var panel=document.getElementById(“”);
变量printWindow=window.open('','',高度=1000,宽度=900');
printWindow.document.write(“”);
printWindow.document.write(“”);
printWindow.document.write(panel.innerHTML);
printWindow.document.write(“”);
printWindow.document.close();
setTimeout(函数(){
printWindow.print();
}, 500);
返回false;
}

将此添加到
linkbutton\u单击

Type myType = this.GetType();
//You may not need this if statement
if (!ClientScript.IsClientScriptBlockRegistered(someType, "_linkbutton_click"))
{
    string script = "PrintPanel;";
    ClientScript.RegisterClientScriptBlock(someType, "_linkbutton_click", script, true);
}

您可以在linkbutton的客户端单击上调用函数


OnClientClick=“return PrintPanel();”
添加到您的
链接按钮中

您不需要。JavaScript是客户端,C#是服务器端。两者不能直接相互参照。你想实现什么?是的,你可以。使用ClientScript.RegisterClientScriptBlock或RegisterStartup。不是直接的,但是你可以。@David我在客户端上工作得很好单击,这只在我的本地pc上工作,在服务器上不工作side@user3012159:这两种环境有什么区别?当这是从服务器上运行时,它以什么方式失败?我没有从服务器上得到任何错误。它只是不工作。是的,这在我的本地pc上工作正常,而不是在服务器上?任何关于原因的线索?使用
单引号
作为
document.getElementById(“”)的
getElementById
对于
pnlContents
控件添加
ClientIDMode=“Static”
并将“pnlContents”用作
文档。getElementById(“pnlContents”)
尝试此解决方案