1. Add Jscript files to the project
//script_1.js-----
function doClick1()
{
alert("OK1_wufeng");
}
//script_2.js-----
function doClick2()
{
alert("OK2");
}
2. In the Solution Explorer, right-click to view the properties of script_1.js and script_2.js, and set the "Generate Operation" property in Advanced to "Embedded Resource".
3. Add the following lines to the AssemblyInfo.cs file: (note the domain name wf.ClientScriptResourceLabel)
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_1.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_2.js", "application/x-javascript")]
4. Add a class to the project, example:
using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;
namespace wf.ClientScriptResourceLabel
{
public class ClientScriptResourceLabel : System.Web.UI.WebControls.WebControl
{
//Call script resources
protected override void OnPreRender(EventArgs e)
{
if (this.Page != null)
{
this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_1.js");
this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_2.js");
}
base.OnPreRender(e);
}
/// <summary>
/// Method for rendering controls RenderContents
/// </summary>
protected override void RenderContents(HtmlTextWriter output)
{
output.AddAttribute("id", "1");
output.AddAttribute("type", "checkbox");
output.AddAttribute("value", "Test 1");
output.AddAttribute("onclick", "javascript:doClick1();");
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
output.AddAttribute("id", "2");
output.AddAttribute("type", "checkbox");
output.AddAttribute("value", "Test 2");
output.AddAttribute("onclick", "javascript:doClick2();");
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
base.RenderContents(output);
}
}
}
You can try it