using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
namespace System.Web.UI.Extensions
{
public static class PageExtension
{
private const char constTabChar = '\t';
private const string constScriptInjectorComment = "\t /* Script injection from server side v. 1.0 improved by Proud Rajabhut Phranakorn University */\n";
private const string constJQueryMainFuncOpenTag = "\t$(function(){\n";
private const string constJQueryMainFuncCloseTag = "\t});\n";
/// <summary>
/// Embeded jQuery library additional command in any page.
/// </summary>
/// <param name="argCorePage"></param>
/// <param name="argScript"></param>
public static void EmbededJQueryScript(this System.Web.UI.Page argCorePage, string argScript)
{
if (argCorePage == null) return;
System.Web.UI.HtmlControls.HtmlGenericControl myHTMLComp
= new System.Web.UI.HtmlControls.HtmlGenericControl("script");
myHTMLComp.Attributes.Add("language", "javascript");
myHTMLComp.Attributes.Add("type", "text/javascript");
myHTMLComp.InnerHtml = "\n"
+ constScriptInjectorComment
+ constJQueryMainFuncOpenTag
+ argScript.ToString()
+ constJQueryMainFuncCloseTag + "\n";
argCorePage.Controls.Add(myHTMLComp);
}
/// <summary>
/// Embeded javascript in any page.
/// </summary>
/// <param name="argCorePage"></param>
/// <param name="argScript"></param>
public static void EmbededJavaScript(this System.Web.UI.Page argCorePage, string argScript)
{
if (argCorePage == null) return;
System.Web.UI.HtmlControls.HtmlGenericControl myHTMLComp
= new System.Web.UI.HtmlControls.HtmlGenericControl("script");
myHTMLComp.Attributes.Add("language", "javascript");
myHTMLComp.Attributes.Add("type", "text/javascript");
myHTMLComp.InnerHtml = "\n"
+ constScriptInjectorComment
+ argScript.ToString()
+ "\n";
argCorePage.Controls.Add(myHTMLComp);
}
/// <summary>
/// Embeded simple jQuery function for test only.
/// </summary>
/// <param name="argCorePage"></param>
public static void EmbededAndTestJQueryScript(this System.Web.UI.Page argCorePage)
{
System.Text.StringBuilder scriptBuilder = new System.Text.StringBuilder();
scriptBuilder.AppendLine(" alert('jQuery(' + $().jquery + ') activated '); ");
EmbededJQueryScript(argCorePage, scriptBuilder.ToString());
}
}
}
}
Usage
อย่าลืม using namespace System.Web.UI.Extensions เข้ามาด้วยค่ะ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Extensions;
public partial class Pages_Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Text.StringBuilder scriptBuilder = new System.Text.StringBuilder();
scriptBuilder.AppendLine(" alert('jQuery(' + $().jquery + ') activated '); ");
this.EmbededJQueryScript( scriptBuilder.ToString());
//OR
//this.EmbededAndTestJQueryScript();
}
}
}
ส่วนการทำ intellisense ของ visual studio ให้ support jQuery ก้อลองไป search ดูค่ะ
เพราะคิดว่าไม่จำเป็น