在CS页面上调用JavaScript函数 [英] call a javascript function on cs page

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

问题描述

你好,


我在下拉列表中使用了javascript函数.我想将此函数页面加载到CS页面上.

我的javascript函数如下:

hello,


I have using a javascript function for dropdownlist.I want to call this function page load on cs page.

my javascript function is given below:

<script type="text/javascript">
        function SetFlag(ddlDuration) {

            if (ddlDuration.value > 0) {
                document.getElementById('<%= inpHide.ClientID %>').value = "true";
            }
            else {
                document.getElementById('<%= inpTemp.ClientID %>').value = "0";
            }
        }

        function Check(ddlDuration) {

            var e = document.getElementById("ctl00_ContentPlaceHolder1_ddlDuration");
            var strUser = e.options[e.selectedIndex].value;
            if (strUser == '0') {
                alert("Please select duration first!");
                return false;
            }
            else {
                return true;
            }
        }
    </script>




如何在CS页面上调用此代码?
请帮助我

谢谢




how to call this on cs page??
plz help me

thank you

推荐答案

在您的CS页面中

In your cs page

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ScriptManager.RegisterStartupScript(this,this.GetType(), "flag", "SetFlag(10);", true);
    }
}


在某种意义上,您绝不能在"C#页面上"调用JavaScript(或者反之亦然).在您的C#代码中,调用JavaScript函数并期望它能正常工作. (相反,使用AJAX可以令人信服地进行伪造,但是,正如该术语所暗示的那样,这是一个异步请求.)

您可以设置服务器页面,以便将内容提供给客户端时,将运行一些JavaScript.为此,已经有几种其他解决方案已经描述过的机制.但这不会不会导致脚本在C#代码中内联运行.在您的情况下,将不会设置ASP.net控件上的属性.

像您这样的问题源自对HTTP的客户端/服务器请求/响应体系结构的基本误解.您的C#代码已完全运行,呈现了一些标记,并将其发送给客户端,并且仅在那时才与JavaScript相关.这不完全是您的错,因为ASP.net(在非MVC模式下)的编写方式尽可能地模糊了边界(我讨厌它的一个原因),例如通过假装Web控件类似于WinForms,并保留该状态.但是,您需要了解服务器脚本会响应客户端请求运行,为请求提供服务并发送回标记,并且客户端脚本(JavaScript)会在请求得到完全响应后在浏览器中运行,并且两者之间的交互仅限于:

  • 服务器端脚本创建包含或引用客户端脚本的标记,这将导致客户端脚本在服务器完全创建并调度响应之后执行;和
  • 发出新的HTTP请求的客户端脚本(通过导航到一个新的URL(如回发)或通过AJAX),这将导致运行新的服务器端脚本(假设请求URL是指一个脚本),它将生成一个新的响应,并且该响应将由客户端处理(作为新页面或在AJAX处理程序过程中).
You can never call JavaScript ''on a C# page'' (or for that matter vice versa), in the sense of, in your C# code, calling a JavaScript function and expecting it to work. (The reverse is possible to fake quite convincingly with AJAX, but, as the term implies, that is an asynchronous request.)

You can set up a server page so that when the content is served to the client, some JavaScript will be run. There are several mechanisms for this which other solutions have described already. But this will not cause the script to run inline in the C# code; in your case the property on the ASP.net control will not be set.

Questions like yours stem from a fundamental misunderstanding of the client/server request/response architecture of HTTP. Your C# code is completely run, some markup is rendered, sent to the client, and only at that point is JavaScript relevant. This is not entirely your fault, because ASP.net (in non-MVC mode) is written in such a way as to obscure the boundary as fully as possible (one reason I hate it), for example by pretending web controls are analogous to WinForms ones, and that state is preserved. But you need to understand that server scripts run in response to a client request, service the request and send back markup, and that client scripts (JavaScript) runs in the browser after a request has been fully responded to, and the interaction between the two is limited to:

  • Server-side scripts creating markup that contains or references client script, which will cause the client script to be executed after the server has fully created and dispatched the response; and
  • Client-side scripts making new HTTP requests (either by navigating to a completely new URL, like postbacks, or through AJAX), which will cause a new server-side script to be run (assuming the request URL refers to a script) that will generate a new response and that response will be processed by the client (either as a new page or in an AJAX handler procedure).


在PageLoad事件背后的代码期间,您无法调用JavaScript(客户端)函数.

您可以在页面加载到浏览器后调用JavaScript.

使用JQuery
You can''t call a JavaScript (client-side) function during the code-behind PageLoad event.

You can call the JavaScript after the page loads in the browser.

Using JQuery


这篇关于在CS页面上调用JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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