如何在c#中调用javascript函数 [英] how to call javascript function in c#

查看:123
本文介绍了如何在c#中调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c中调用javascript函数#



c#代码背后:

how to call javascript function in c#

c# code behind:

protected void print_click(object sender,eventargs e)
{
  //When i clicked this button i need to call javascript function
}





javascript code





javascript code

function example()
{
var prtContent = document.getElementById('<%=GridView3.ClientID %>');
//in between these two lines many lines there its not necessary here
window.print();
}

推荐答案





你可以执行javascript文件使用,



ScriptManager.RegisterStartupScript [ ^ ]



示例:如何在C#中执行javascript [ ^ ]



还有其他方式。如果您不可行,请告诉我们,



谢谢

-Amit。
Hi,

You can execute javascript file using,

ScriptManager.RegisterStartupScript[^]

Example: How to execute javascript in C#[^]

There are other way as well. Let us know if it is not feasible for you,

Thanks
-Amit.


以这种方式写函数

Write function this way
protected void print_click(object sender,eventargs e)
{
  //when i click this button i need to call javascript function
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script language='javascript'>");
            sb.Append(@"example();");
            sb.Append(@"</script>");
     System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);
}


阅读所有现有答案,但首先要了解一个基本的事情:你甚至不能谈论电话。



C#在服务器端,Javascript - 在客户端。它可能只是呼唤。它是什么?首先,您需要了解Web功能的一些基础知识。所有服务器端代码都从客户端接收HTTP请求,处理它,然后生成HTTP响应并将其发送到客户端。您可以生成任何可思考的内容。部分内容可以是Javascript。您只需使用一些数据以参数化方式生成Javascript。你是怎么做到的?这是一个单独的问题。您可以直接生成HTTP响应,也可以使用 RegisterStartupScript 或其他任何内容。这是它如何运作的本质。



-SA
Read all of the existing answers, but first, understand one fundamental things: you cannot even talk about "call".

C# is on the server side, Javascript — on the client side. It could be anything but "call". What is it? First of all, you need to understand some basics of the Web functionality. All server-side code does is receiving HTTP request from client side, process it, and then generate HTTP response and send it to client side. You can generate any thinkable content. And part of the content can be Javascript. You just generate Javascript, possibly in a parametrized way, using some data. How you do it? This is a separate question. You can generate HTTP response directly, or use RegisterStartupScript, or anything. This is the essence of how it works.

—SA


这篇关于如何在c#中调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆