將JS指令碼內嵌到組件中的幾種方法

來源:互聯網
上載者:User

將JS指令碼內嵌到組件中的幾種方法:

  1.開啟VS,建立一個WebApplication1項目[這裡建立網站也可以],採用預設設定。

  2.在當前解決方案中添加新項目。(類庫: JSLib)

  添加後,預設的會有一個Class.cs檔案,我們將其重新命名為:【ReferenceClientScript.cs】,這在後面我們會用它。

  3.由於建立的是類庫,所以要添加引用:System.Web,這在後我們會用到。

  4.在JSLib中添加一個JS檔案,取預設設定。自動有一個JScript1.js檔案。

  5.編寫JS指令碼【JScript.js】。

  我們這裡只是一個簡單的示範,所以指令碼代碼盡量簡單。Hello,World!,一段最簡單的類,目的在示範,當然你可以寫的更複雜些。

  JSScript.js 代碼:

以下是程式碼片段:
  function Hello(){
  alert("Hello,world!");
  }

  6.設定JScript.js屬性->產生操作為【內嵌資源】

  7.在JSLib項目的AssemblyInfo.cs檔案最後添加一行代碼:【注意JSLib.JScript1.js,JSLib是js所在項目的命名空間】

  [assembly: WebResource("JSLib.JScript1.js", "text/javascript")]

  8.編寫JS指令碼註冊類【ReferenceClientScript.cs】。

  ReferenceClientScript.cs 代碼:

以下是程式碼片段:

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  namespace JSLib
  { www.yzjjx.com
  public class ReferenceClientScript : System.Web.UI.WebControls.WebControl
  {
  protected override void OnPreRender(EventArgs e)
  {
  if (this.Page != null)
  {
  ClientScriptManager manager = this.Page.ClientScript;
  manager.RegisterClientScriptResource(typeof(ReferenceClientScript), "JSLib.JScript1.js");
  }
  base.OnPreRender(e);
  }
  }
  }

  9.在WebApplication1中添加項目引用。編譯後產生JSLib.dll

  10.在要呼叫指令碼的頁面註冊JSLib.dll

以下是程式碼片段:
<%@ Register Assembly="JSLib" Namespace="JSLib" TagPrefix="JSLibInstance" %>
  

 11.引用指令碼。

以下是程式碼片段:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> yzyedu.com

<%@ Register Assembly="JSLib" Namespace="JSLib" TagPrefix="JSLibInstance" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>無標題頁</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<div>

<JSLibInstance:ReferenceClientScript id="js1" runat="server"></JSLibInstance:ReferenceClientScript>

  JS封裝到DLL 並在頁面調用 Demo。

Clenbuterol Hydrochloride www.chouclub.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.