刚刚学习自定义控件,一个按钮单击时,希望用javascript弹出alert(‘click’);并且这句话是在一个js文件里面,我试着写了代码,但是不对,高手,please give me a hand
自定义控件的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.HtmlControls;namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:usejs runat=server></{0}:usejs>")]
public class usejs : WebControl,ICallbackEventHandler
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
} set
{
ViewState["Text"] = value;
}
}
public string ClientPath
{
get
{
string s = (string)ViewState["ClientPath"];
return ((s == null) ? string.Empty : s);
}
set
{
ViewState["ClientPath"] = value;
}
}
public string GetCallbackResult()
{
return this.Text.ToString();
}
public void RaiseCallbackEvent(string eventArgument)
{ }
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string strJSPath = base.ResolveUrl(Path.Combine(this.ClientPath,@"Js\alertfuntion.js"));
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(),"ControlJs"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ControlJs", "<script type='text/javascript' src='" + strJSPath + "'></script>",false);
}
string strCallbackfunction = "fn";
string strCallBackReference = Page.ClientScript.GetCallbackEventReference(this, "", strCallbackfunction, "");
string strCallbackScript = "function executeCallBack(){" + strCallBackReference + ";}"; if (Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(),"executeCallBack")==false)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "executeCallBack", strCallbackScript, true);
}
}
protected override void Render(HtmlTextWriter writer)
{
HtmlGenericControl bt = new HtmlGenericControl("Button");
bt.ID = "btn";
bt.Attributes["onclick"] = "executeCallBack();";
bt.RenderControl(writer);
base.Render(writer);
}
}
}
alertfunction.js文件里面的内容很简单
// JScript 文件function fn() {
alert('click');
}
生成控件没有错,但是就是执行单击的时候,出现不了alert(‘click’);
自定义控件的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.HtmlControls;namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:usejs runat=server></{0}:usejs>")]
public class usejs : WebControl,ICallbackEventHandler
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
} set
{
ViewState["Text"] = value;
}
}
public string ClientPath
{
get
{
string s = (string)ViewState["ClientPath"];
return ((s == null) ? string.Empty : s);
}
set
{
ViewState["ClientPath"] = value;
}
}
public string GetCallbackResult()
{
return this.Text.ToString();
}
public void RaiseCallbackEvent(string eventArgument)
{ }
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string strJSPath = base.ResolveUrl(Path.Combine(this.ClientPath,@"Js\alertfuntion.js"));
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(),"ControlJs"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ControlJs", "<script type='text/javascript' src='" + strJSPath + "'></script>",false);
}
string strCallbackfunction = "fn";
string strCallBackReference = Page.ClientScript.GetCallbackEventReference(this, "", strCallbackfunction, "");
string strCallbackScript = "function executeCallBack(){" + strCallBackReference + ";}"; if (Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(),"executeCallBack")==false)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "executeCallBack", strCallbackScript, true);
}
}
protected override void Render(HtmlTextWriter writer)
{
HtmlGenericControl bt = new HtmlGenericControl("Button");
bt.ID = "btn";
bt.Attributes["onclick"] = "executeCallBack();";
bt.RenderControl(writer);
base.Render(writer);
}
}
}
alertfunction.js文件里面的内容很简单
// JScript 文件function fn() {
alert('click');
}
生成控件没有错,但是就是执行单击的时候,出现不了alert(‘click’);
我也才开始学自定义控件. 疑惑中.....