WebPart 注册js

来源:互联网 发布:三国志13数据 编辑:程序博客网 时间:2024/06/07 20:03

 如何在webpart中引用js喝使用js,可能困扰着不少人, 其实webpart中关于javascript的使用有很多种方法的,WebPart中注册JavaScript,WebPart中嵌入js,首先介绍一下再webpart页面中直接写入的代码如下:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Text;
using System.Web.UI.HtmlControls;

namespace RegisterJsAndCSS
{
    [Guid("7d4c0f63-6ffe-44aa-88be-6f798e9bd5ac")]
    public class RegisterJsCs : System.Web.UI.WebControls.WebParts.WebPart
    {
        #region
        StringBuilder strbuilder = null;
        HtmlContainerControl htmldiv = null;
        
        Table table = null;
        TableRow tr = null;
        TableCell td = null;
        TextBox tx = null;
        Button bt = null;
      
        #endregion
        public RegisterJsCs()
        {
            table = new Table();
            tr = new TableRow();
            tx = new TextBox();
            td = new TableCell();
            strbuilder = new StringBuilder();
            htmldiv = new HtmlGenericControl();
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CreateUI();
        }
     
        protected void CreateUI()
        {
          

            td = new TableCell();          
            tx.Text = "mnnnr...";
            tx.ID= "LALA";
            td.Controls.Add(tx);

            bt = new Button();
            bt.Text = "点击";
            bt.ID = "shishi";
            bt.Attributes.Add("onmouseover", "aa()");
            td.Controls.Add(bt);
            tr.Cells.Add(td);
            table.Rows.Add(tr);
            this.Controls.Add(table);

            //嵌入js代码
            strbuilder.Append(@"<script type='text/javascript'>
            function aa()
              {
                 alert('弹出对话框!!!');     
              }
               </script>");
            htmldiv.InnerHtml = strbuilder.ToString();
            this.Controls.Add(htmldiv);
        }
    
    }
}
下面一种是注册js的代码:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Text;
using System.Web.UI.HtmlControls;

namespace RegisterJsAndCSS
{
    [Guid("7d4c0f63-6ffe-44aa-88be-6f798e9bd5ac")]
    public class RegisterJsCs : System.Web.UI.WebControls.WebParts.WebPart
    {
        #region
 
        Table table = null;
        TableRow tr = null;
        TableCell td = null;
        TextBox tx = null;
        Button bt = null;
        public const string Resource_Path = "~/_layouts/codeArt/";
        public const string Controltemplaters = "~/_controltemplates/";
        #endregion
        public RegisterJsCs()
        {
            table = new Table();
            tr = new TableRow();
            tx = new TextBox();
            td = new TableCell();
         }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CreateUI();
        }
        protected override void OnPreRender(EventArgs e)
        {
            this.RegisterCommonJs("Yutou.js");
            base.OnPreRender(e);
        }
        protected void CreateUI()
        {
          

            td = new TableCell();          
            tx.Text = "mnnnr...";
            tx.ID= "LALA";
            td.Controls.Add(tx);

            bt = new Button();
            bt.Text = "点击";
            bt.ID = "shishi";
            bt.Attributes.Add("onmouseover", "Reisterjj()");
            td.Controls.Add(bt);
            tr.Cells.Add(td);
            table.Rows.Add(tr);
            this.Controls.Add(table);
          
        }
        /// <summary>
        /// 注册js脚本
        /// </summary>
        /// <param name="jsFileName"></param>
        protected void RegisterCommonJs(string jsFileName)
        {
            string jsPath = base.ResolveUrl(Resource_Path) + jsFileName;
            Page.ClientScript.RegisterClientScriptInclude(typeof(RegisterJsCs),jsFileName.ToLower(),jsPath);
        }
    }
}
js文件的代码:

function Reisterjj()
{  
     alert("Don't touch me!!!");
}
js文件要放在本地的12/layouts/codeArt的文件下,就OK了....

原创粉丝点击