📄 mylabel.cs
字号:
using System;
using System.Web;
using System.Web.UI;
namespace snowy
{
public class MyLabel : Control, IPostBackEventHandler
{
// Define a Server-side event click
public event EventHandler Click;
string HiddenCountID
{
get{ return "__" + this.ClientID + "_Count"; }
}
string HiddenTextID
{
get{ return "__" + this.ClientID + "_Text"; }
}
public int Count
{
get
{
object obj = ViewState["Count"];
return (obj == null) ? 0 : (int)obj;
}
set { ViewState["Count"] = value; }
}
public string Text
{
get
{
object obj = ViewState["Text"];
return (obj == null) ? String.Empty : (string)obj;
}
set { ViewState["Text"] = value; }
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Page != null)
{
Page.RegisterHiddenField(HiddenCountID, Count.ToString());
Page.RegisterHiddenField(HiddenTextID, Text.ToString());
}
string script = "<script language=javascript>\n";
script += " function showcount()\n";
script += " {\n";
script += " document.all[\"" + this.ClientID + "\"].innerText = 'You have clicked ' + document.all[\"" + HiddenCountID + "\"].value + ' times';\n";
script += " }\n";
script += " function showtext()\n";
script += " {\n";
script += " document.all[\"" + this.ClientID + "\"].innerText = document.all[\"" + HiddenTextID + "\"].value;\n";
script += " }\n";
script += " </script>";
if(!Page.IsClientScriptBlockRegistered("mylabelscript"))
Page.RegisterClientScriptBlock("mylabelscript", script);
}
public void RaisePostBackEvent(string eventArgument)
{
Count++;
OnClick(EventArgs.Empty);
}
protected void OnClick(EventArgs arg)
{
if(Click != null)
Click(this, arg);
}
protected override void Render(HtmlTextWriter writer)
{
string postback = "";
if (Page != null)
postback = Page.GetPostBackEventReference(this) + ";";
string id = "id=\"" + this.ClientID + "\"";
string click = "onclick=\"" + postback + "\"";
string mouseEnter = "onmouseenter=\"showcount();\"";
string mouseLeave = "onmouseleave=\"showtext();\"";
writer.Write("<span " + id + " " + click + " " + mouseEnter + " " + mouseLeave + ">" + this.Text + "</span>");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -