如何从文件后面的.net代码编写javascript。 [英] How to write a javascript from .net code behind file.

查看:77
本文介绍了如何从文件后面的.net代码编写javascript。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个用户控件假设a.ascx,我想在文件后面的用户控件代码中写一个javascript文件。可以任何一个请转换下面的javascript文件从后面的代码调用。



Javascript文件



< pre lang =Javascript> function xrxIdentifyCurrentBrowser(){
var uaString = 导航 .userAgent;
return (((uaString!= undefined )&&(uaString!= < span class =code-keyword> null
))?(((uaString = uaString.toLowerCase())。indexOf( galio)> = 0 )?
FirstGenBrowser :((uaString.indexOf( webkit)> = 0 )? SecondGenBrowser 未知)): 无用户代理);
}

解决方案

我也试图帮助你解决你问题的Stack Overflow线程,问题实际上是你实际上不应该要求服务器端代码接管客户端代码。如果必须,那么您必须在启动时注册代码然后执行它。



但是,我在那里提供的另一个解决方案是你只需要确定浏览器。然后,您可以很好地使用 Request.Browser 对象来检查浏览器的属性。它将为您提供有关浏览器本身的详细信息,并可用于服务器端C#代码。



否则,解决方案1所说的JavaScript代码必须在客户端代码环境中执行(仅限JavaScript)。您可以组成一个文件,并在实际的HTML DOM中引用该文件,或者您可以在HTML DOM中的脚本元素中编写该文件。但是,使用该Request.Browser对象在服务器端执行所有这些检查的效率仍然不高。



你可以阅读 MSDN文档 [ ^ ]关于Request.Browser和不同的成员吧暴露让你在确定浏览器时工作。



如何:检测ASP.NET中的浏览器类型和浏览器功能 Web窗体(相同的基础代码;不要惊慌) [ ^ ]


I相信代码隐藏文件只能包含C#或VB.NET中的服务器端代码。你有2个选择:



  1. 将你的功能嵌入到你的页面中:

     <   script    类型  =  text / javascript >  
    function xrxIdentifyCurrentBrowser(){
    var uaString = navigator .userAgent;
    return (((uaString!= undefined )&&(uaString!= < span class =code-keyword> null ))?(((uaString = uaString.toLowerCase())。indexOf( galio)> = 0 )?
    FirstGenBrowser :((uaString.indexOf( webkit)> = 0 )? SecondGenBrowser 未知)): 无用户代理);
    }
    < / 脚本 >

  2. 在站点文件夹中的某处创建包含您的函数的JS文件并插入像这样进入你的页面

     <   script     type   =  text / javascript    src   =  yourfile.js >  <   /   script   >  


Hi i have an user control suppose a.ascx, i want to write a javascript file in user control code behind file. Can any one please convert the below javascript file to call from code behind.

Javascript file

function xrxIdentifyCurrentBrowser() {
       var uaString = navigator.userAgent;
       return (((uaString != undefined) && (uaString != null)) ? (((uaString = uaString.toLowerCase()).indexOf("galio") >= 0) ?
           "FirstGenBrowser" : ((uaString.indexOf("webkit") >= 0) ? "SecondGenBrowser" : "Unknown")) : "No User Agent");
   }

解决方案

I also tried to help you out on that Stack Overflow thread of your question, the problem actually is that you shouldn't actually ask the server-side code to take over the client-side code too. If you have to then you have to register the code at the start-up and then execute it.

But, another solution, which I provided there too, is that you need to determine the browser only. Then you can make a good use the Request.Browser object, to check the properties of the browser. It would give you details about the browser itself, and can be used in the server-side C# code.

Otherwise, the JavaScript code as Solution 1 says, must be executed in the client-side code environment (by JavaScript only). You can make up a file, and reference that file in your actual HTML DOM, or you can write that in a script element in your HTML DOM. But still it won't be as much efficient as it can be to perform all these checks at the server-side using that Request.Browser object.

You can read the MSDN document[^] about the Request.Browser and different members it exposes for you to work out in determining the browsers.

How to: Detect Browser Types and Browser Capabilities in ASP.NET "Web Forms (Same underlying code; don't panic)"[^]


I believe that code-behind files can only contain server side code in C# or VB.NET. You have 2 options:


  1. Embed your function into your page like this:

    <script type="text/javascript">
    function xrxIdentifyCurrentBrowser() {
      var uaString = navigator.userAgent;
      return (((uaString != undefined) && (uaString != null)) ? (((uaString = uaString.toLowerCase()).indexOf("galio") >= 0) ?
                "FirstGenBrowser" : ((uaString.indexOf("webkit") >= 0) ? "SecondGenBrowser" : "Unknown")) : "No User Agent");
        }
    </script>

  2. Create a JS file containing your functions somewhere inside your site folder and insert it into your page like this

    <script type="text/javascript" src="yourfile.js"></script>


这篇关于如何从文件后面的.net代码编写javascript。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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