结合MS AJAX将js文件编译到动态链接库

2016-02-19 21:46 10 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐结合MS AJAX将js文件编译到动态链接库,赶紧看过来吧!

【 tulaoshi.com - Web开发 】

  为了使javascript代码不被窃取,我们可以将js文件编译成动态链接库(dll)文件。下面为了演示这一功能,创建了一个控件。

   程序代码:http://www.cnblogs.com/Files/hblynn/SampleControlsCS.rar

  一、创建一个类库项目,命名为UpdateAnimate。

  二、向项目中添加引用System.Web, System.Drawing, System.Web.Extensions

  三、向项目中添加一个Jscript的文件UpdatePanelAnimation.js

  四、向文件中添加如下代码:

  
  BorderAnimation = function(color)
  {
      this._color = color;
  }

  BorderAnimation.prototype =
  {
      animate: function(panelElement)
      {
          var s = panelElement.style;
          s.borderWidth = '2px';
          s.borderColor = this._color;
          s.borderStyle = 'solid';

          window.setTimeout(
              function()
              {
              {
                  s.borderWidth = 0;
              }
              },
              500);
      }
  }

  
  这段代码中,包含一段临时改变UpdatePanel控件样式的方法

   

  五、解决方案资源管理器中,右键查看UpdatePanelAnimation.js的属性,把高级中的“生成操作”属性设置成“嵌入的资源”。

  六、向项目中添加一个类CustomControl

  七、替换类中的代码:

  using System;
  using System.Drawing;
  using System.Web.UI;
  using System.Web;
  using System.Globalization;

  namespace UpdateAnimate
  {
      public class UpdatePanelAnimationWithClientResource : Control
      {
          private string _updatePanelID;
          private Color _borderColor;
          private Boolean _animate;
          public Color BorderColor
          {
              get
              {
                  return _borderColor;
              }
              set
              {
                  _borderColor = value;
              }
          }

          public string UpdatePanelID
          {
              get
              {
                  return _updatePanelID;
              }
              set
              {
                  _updatePanelID = value;
              }
          }

          public Boolean Animate
          {
              get
              {
                  return _animate;
              }
              set
              {
                  _animate = value;
              }
          }
          protected override void OnPreRender(EventArgs e)
          {
              base.OnPreRender(e);
              if (Animate)
              {

                  UpdatePanel updatePanel = (UpdatePanel)FindControl(UpdatePanelID);

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

                  string script = String.Format(
                     CultureInfo.InvariantCulture,
                     @"
  Sys.Application.add_load(function(sender, args) {{
  var {0}_borderAnimation = new BorderAnimation('{1}');   
  var panelElement = document.getElementById('{0}');
       if (args.get_isPartialLoad()) {{
          {0}_borderAnimation.animate(panelElement);
      }}
  }})
  ",
                     updatePanel.ClientID,
                     ColorTranslator.ToHtml(BorderColor));

  
                  ScriptManager.RegisterStartupScript(
                      this,
                      typeof(UpdatePanelAnimationWithClientResource),
                      ClientID,
                      script,
                      true);
              }
          }
      }
  }

   

   

  八、向AssemblyInfo.cs文件中添加如下行:

  [assembly: System.Web.UI.WebResource("UpdateAnimate.UpdatePanelAnimation.js", "application/x-javascript")]

  九、生成项目。

   

  控件演示:

  一、创建一个AJAX-enabled类型的网站项目。

  二、向网站跟目录下添加bin目录。

  三、从控件项目的binDebug或 binRelease目录拷贝UpdateAnimate.dll到网站bin目录里。

  四、替换Default.aspx的内容并运行程序:

   

    %@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %

  %@ Register TagPrefix="Samples" Namespace="UpdateAnimate" Assembly="UpdateAnimate" %

  !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

  script runat="server"

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  /script

  html xmlns="http://www.w3.org/1999/xhtml"
  head id="Head1" runat="server"
      titleScriptReference/title
  /head
  body
      form id="form1" runat="server"
          div
              asp:ScriptManager ID="ScriptManager1"
                                   EnablePartialRendering="True"
                                   runat="server"
               Scripts
                  asp:ScriptReference Assembly="UpdateAnimate" Name="UpdateAnimate.UpdatePanelAnimation.js" /
               /Scripts
              /asp:ScriptManager

  
              Samples:UpdatePanelAnimationWithClientResource
                       ID="UpdatePanelAnimator1"
                       BorderColor="Green"
                       Animate="true"
                       UpdatePanelID="UpdatePanel1"
                       runat="server"
              /Samples:UpdatePanelAnimationWithClientResource
              asp:UpdatePanel ID="UpdatePanel1"
                                 UpdateMode="Conditional"
                                 runat="server"
                  ContentTemplate
                      asp:Calendar ID="Calendar2"
                                    runat="server"
                      /asp:Calendar
                  /ContentTemplate
              /asp:UpdatePanel
          /div
      /form
  /body
  /html
  http://www.cnblogs.com/hblynn/archive/2007/02/01/637312.html

来源:https://www.tulaoshi.com/n/20160219/1627092.html

延伸阅读
虽然能用DLL实现的功能都可以用COM来替代,但DLL的优点确实不少,它更容易创建。本文将讨论如何利用VC MFC来创建不同类型的DLL,以及如何使用他们。 一、DLL的不同类型 使用VC++可以生成两种类型的DLL:MFC扩展DLL和常规DLL。常规DLL有可以分为动态连接和静态连接。Visual C++还可以生成WIN32 DLL,但不是这里讨论的主要对象。 1、MFC扩展DL...
1.Windows的动态链接库原理 动态链接库(DLLs)是从C语言函数库和Pascal库单元的概念发展而来的。所有的C语言标准库函数都存放在某一函数库中,同时用户也可以用LIB程序创建自己的函数库。在链接应用程序的过程中,链接器从库文件中拷贝程序调用的函数代码,并把这些函数代码添加到可执行文件中。这 种方法同只把函数储存在已...
从前文可知,DLL在程序编制中可作出巨大贡献,它提供了具共性代码的复用能力。但是,正如一门高深的武学,若被掌握在正义之侠的手上,便可助其仗义江湖;但若被掌握在邪恶之徒的手上,则必然在江湖上掀起腥风血雨。DLL正是一种这样的武学。DLL一旦染上了魔性,就不再是正常的DLL程序,而是DLL木马,一种恶贯满盈的病毒,令特洛伊一夜之间国...
摘要 文中介绍了一种实现VB调用C语言程序的方法-动态连接库法,给出了动态链接库制作的一般框架,通过实例描述了动态连接库的实现及其制做方法、编程步骤,这种方法具有普遍的意义。 关键词:VB,动态连接库DLL,C程序 一、引言 VisualBasic(以下简称VB)是一种可视化的面向对象的编程语言,以其编程简单、快捷等特点,深受广...
3  利用DLLs实现数据传输 3.1 DLLs中的全局内存 Windows规定:DLLs并不拥有它打开的任何文件或它分配的任何全局内存块。这些对象由直接或间接调用DLLs的应用程序拥有。这样,当应用程序中止时,它拥有的打开的文件自动关闭,它拥有的全局内存块自动释放。这就意味着保存在DLLs全局变量中的文件和全局内存块...

经验教程

798

收藏

65
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部