首页 新闻 会员 周边

asp.net 给 Label添加一个 点击事件

0
悬赏园豆:10 [已解决问题] 解决于 2010-01-05 18:45
1 using System;
2  using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.UI.WebControls;
6 using System.Web.UI;
7
8 namespace JasonXu_LabelEvent
9 {
10

12 class Labelbtn:Label,IPostBackEventHandler
13 {
14 public event EventHandler Click;
15
16 protected void onClick(EventArgs e)
17 {
18 if (Click != null)
19 {
20 Click(this, e);
21 }
22 }
23
24
25 void RaisePostBackEvent(string eventArgument)
26 {
27 onClick(new EventArgs());
28 }
29
30
31 protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
32 {
33 base.AddAttributesToRender(writer);
34 writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "onClick"));
35 }
36
37 }
38 }
39

 

我想给 asp.net 的Label添加一个点击事件

不知道 上面代码那里有错, 而且不能拖到界面上,也不知道事件能不能用,大家帮忙解决一下。最好

给个例子

JasonXu的主页 JasonXu | 初学一级 | 园豆:195
提问于:2009-12-30 12:17
< >
分享
最佳答案
0

不太清楚了,哈哈,看看Button是怎么做的?

代码
public class Button : WebControl, IButtonControl, IPostBackEventHandler
{
// 摘要:
// 初始化 System.Web.UI.WebControls.Button 类的新实例。
public Button();

// 摘要:
// 获取或设置一个值,该值指示在单击 System.Web.UI.WebControls.Button 控件时是否执行验证。
//
// 返回结果:
// 如果在单击 System.Web.UI.WebControls.Button 控件时执行验证,则为 true;否则为 false。默认值为 true。
[DefaultValue(true)]
[Themeable(
false)]
public virtual bool CausesValidation { get; set; }
//
// 摘要:
// 获取或设置可选参数,该参数与关联的 System.Web.UI.WebControls.Button.CommandName 一起被传递到 System.Web.UI.WebControls.Button.Command
// 事件。
//
// 返回结果:
// 与关联的 System.Web.UI.WebControls.Button.CommandName 一起被传递到 System.Web.UI.WebControls.Button.Command
// 事件的可选参数。默认值为 System.String.Empty。
[Bindable(true)]
[DefaultValue(
"")]
[Themeable(
false)]
public string CommandArgument { get; set; }
//
// 摘要:
// 获取或设置命令名,该命令名与传递给 System.Web.UI.WebControls.Button.Command 事件的 System.Web.UI.WebControls.Button
// 控件相关联。
//
// 返回结果:
// System.Web.UI.WebControls.Button 控件的命令名。默认值为 System.String.Empty。
[Themeable(false)]
[DefaultValue(
"")]
public string CommandName { get; set; }
//
// 摘要:
// 获取或设置在引发某个 System.Web.UI.WebControls.Button 控件的 System.Web.UI.WebControls.Button.Click
// 事件时所执行的客户端脚本。
//
// 返回结果:
// 在引发某个 System.Web.UI.WebControls.Button 控件的 System.Web.UI.WebControls.Button.Click
// 事件时所执行的客户端脚本。
[Themeable(false)]
[DefaultValue(
"")]
public virtual string OnClientClick { get; set; }
//
// 摘要:
// 获取或设置单击 System.Web.UI.WebControls.Button 控件时从当前页发送到的网页的 URL。
//
// 返回结果:
// 单击 System.Web.UI.WebControls.Button 控件时从当前页发送到的网页的 URL。默认值为空字符串 (""),表示将页回发到自身。
[DefaultValue("")]
[Editor(
"System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Themeable(
false)]
[UrlProperty(
"*.aspx")]
public virtual string PostBackUrl { get; set; }
//
// 摘要:
// 获取或设置在 System.Web.UI.WebControls.Button 控件中显示的文本标题。
//
// 返回结果:
// 在 System.Web.UI.WebControls.Button 控件中显示的文本标题。默认值为 System.String.Empty。
[DefaultValue("")]
[Localizable(
true)]
[Bindable(
true)]
public string Text { get; set; }
//
// 摘要:
// 获取或设置一个布尔值,该值指示 System.Web.UI.WebControls.Button 控件使用客户端浏览器的提交机制还是 ASP.NET
// 回发机制。
//
// 返回结果:
// 如果该控件使用了客户端浏览器的提交机制,则为 true;否则为 false。默认为 true。
[DefaultValue(true)]
[Themeable(
false)]
public virtual bool UseSubmitBehavior { get; set; }
//
// 摘要:
// 获取或设置在 System.Web.UI.WebControls.Button 控件回发到服务器时要进行验证的控件组。
//
// 返回结果:
// 在 System.Web.UI.WebControls.Button 控件回发到服务器时要进行验证的控件组。默认值为空字符串 ("")。
[DefaultValue("")]
[Themeable(
false)]
public virtual string ValidationGroup { get; set; }

// 摘要:
// 在单击 System.Web.UI.WebControls.Button 控件时发生。
public event EventHandler Click;
//
// 摘要:
// 在单击 System.Web.UI.WebControls.Button 控件时发生。
public event CommandEventHandler Command;

// 摘要:
// 将 System.Web.UI.WebControls.Button 控件的属性添加到输出流用以在客户端上呈现内容。
//
// 参数:
// writer:
// 一个 System.Web.UI.HtmlTextWriter,其中包含要在客户端上呈现内容的输出流。
protected override void AddAttributesToRender(HtmlTextWriter writer);
//
// 摘要:
// 创建表示 System.Web.UI.WebControls.Button 控件的回发行为的 System.Web.UI.PostBackOptions
// 对象。
//
// 返回结果:
// 一个表示 System.Web.UI.WebControls.Button 控件的回发行为的 System.Web.UI.PostBackOptions。
protected virtual PostBackOptions GetPostBackOptions();
//
// 摘要:
// 引发 System.Web.UI.WebControls.Button 控件的 System.Web.UI.WebControls.Button.Click
// 事件。
//
// 参数:
// e:
// 包含事件数据的 System.EventArgs。
protected virtual void OnClick(EventArgs e);
//
// 摘要:
// 引发 System.Web.UI.WebControls.Button 控件的 System.Web.UI.WebControls.Button.Command
// 事件。
//
// 参数:
// e:
// 包含事件数据的 System.Web.UI.WebControls.CommandEventArgs。
protected virtual void OnCommand(CommandEventArgs e);
//
// 摘要:
// 确定按钮在客户端上呈现之前是否已被单击。
//
// 参数:
// e:
// 包含事件数据的 System.EventArgs 对象。
protected internal override void OnPreRender(EventArgs e);
//
// 摘要:
// 当 System.Web.UI.WebControls.Button 控件回发到服务器时引发此控件的事件。
//
// 参数:
// eventArgument:
// 该事件的参数。
protected virtual void RaisePostBackEvent(string eventArgument);
//
// 摘要:
// 将控件的内容呈现到指定的编写器中。
//
// 参数:
// writer:
// System.Web.UI.HtmlTextWriter,表示要在客户端呈现 HTML 内容的输出流。
protected internal override void RenderContents(HtmlTextWriter writer);
}

IButtonControl接口:

代码
// 摘要:
// 定义必须实现的属性和事件以允许控件在网页上的作用就像按钮一样。
public interface IButtonControl
{
// 摘要:
// 获取或设置指示单击按钮是否会导致发生页验证的值。
//
// 返回结果:
// 如果单击按钮会导致发生页验证,则为 true;否则,为 false。
bool CausesValidation { get; set; }
//
// 摘要:
// 获取或设置传播到 System.Web.UI.WebControls.IButtonControl.Command 事件的可选参数。
//
// 返回结果:
// 传播到 System.Web.UI.WebControls.IButtonControl.Command 事件的参数。
string CommandArgument { get; set; }
//
// 摘要:
// 获取或设置传播到 System.Web.UI.WebControls.IButtonControl.Command 事件的命令名称。
//
// 返回结果:
// 传播到 System.Web.UI.WebControls.IButtonControl.Command 事件的命令的名称。
string CommandName { get; set; }
//
// 摘要:
// 获取或设置单击按钮控件时从当前页发送到的网页的 URL。
//
// 返回结果:
// 单击按钮控件时从当前页发送到的网页的 URL。
string PostBackUrl { get; set; }
//
// 摘要:
// 获取或设置为该按钮显示的文本标题。
//
// 返回结果:
// 为该按钮显示的文本标题。
string Text { get; set; }
//
// 摘要:
// 获取或设置按钮控件回发到服务器时所导致验证的控件组的名称。
//
// 返回结果:
// 按钮控件回发到服务器时所导致验证的控件组的名称。
string ValidationGroup { get; set; }

// 摘要:
// 在单击按钮控件时发生。
event EventHandler Click;
//
// 摘要:
// 在单击按钮控件时发生。
event CommandEventHandler Command;
}
收获园豆:7
OOLi | 初学一级 |园豆:163 | 2009-12-30 14:02
你能给说明一下?上面的代码没有做到?
JasonXu | 园豆:195 (初学一级) | 2009-12-30 14:22
其他回答(2)
0

想实现什么功能,为什么非得给Label 加事件, LinkButton不行吗?

Annie | 园豆:396 (菜鸟二级) | 2009-12-30 14:16
0

this.Lable1.Attributes.Add("onclick","return XX()"); 在加载事件中 加上这句话,XX()是你的单击方法

收获园豆:3
死神的背影 | 园豆:667 (小虾三级) | 2009-12-30 16:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册