ASP.NET web的自定义用户控件以及相关问题

今天是情人节,老天很不错,没有赐予我美女一名,却让我解决了困惑我很久的问题。

心情高兴,就发出来 让和我一样苦苦寻找真理的朋友···咳咳 你们懂的

我用的工具是 VS2010

一直以来都在困惑 为什么我的自定义用户控件不能在页面上触发事件然后 为什么别人的就可以。

于是花费 了3天找资料,明白了委托事件。

开始用委托事件 ,的确可以达到我的目的。不过页面传回···而且··貌似效率不咋地·····

于是继续找资料

终于找到了一个 为什么 我那个 用户控件不能生成事件 不能 触发事件的原因。

这个资料具体的地址忘了。

自定义用户控件 拖放在页面上  在页面的代码设计器上 在此控件 的 事件里面 找不到 在控件中定义的事件。

光说文字看不懂 。来看图说话

这个是我定义的事件  onClick

这是我在页面上控件 事件里面找 但是没有找到我定义的事件。

我看到其他的前辈都是这样的做的,为什么就我的不行···

View Code
在页面上为onClick事件添加处理方法
<ascx:UserControl1 ID="UserControl11" runat="server" onClick="uc1_OnClick"/>
publicvoid uc1_onClick(object sender, EventArgs args)
{
Property property
= (Property)args;
Response.Write(property.Count.ToString());
}

于是在网上找了下 突然发现个资料里面  在 定义的事件的名字前面加了一个On  。于是我对比我的,发现我定义的 onClick  居然也有一个 OnonClick 事件 ,于是赶紧注册事件。

一测试,哈哈 果然行了。

View Code
protectedvoid cc_Click(object sender, EventArgs e)
{
Label1.Text
= cc.Num.ToString();
}

话说能是能把我的功能实现了。

可为什么 还要在我自己定义的事件前面加 On 关键字? 还不明白? 如何给这样的控制设置一个默认的事件,在页面上双击这个控件就 启用这个事件?

还有就是 这样的自定义用户控件的属性

以及把事件显示出来?

 ——————————————————————————————————————————————————————————————————

 下面是我做的代码,我这边能完全跑出来  (献丑了···)

效果

目录

 

——————————————————————————————以下是代码

ConputControl 部分

前台

View Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ConputControl.ascx.cs" Inherits="ConputControl"%>
<div style=" width:126px; ">
<asp:Button ID="But_low" runat="server" Text=" - " onclick="But_low_Click"/>&nbsp;
<asp:Label ID="Lab_show" runat="server" Text="1"></asp:Label>&nbsp;
<asp:Button ID="But_add" runat="server" Text=" + " onclick="But_add_Click"/>
</div>

后台 

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

publicpartialclass ConputControl : System.Web.UI.UserControl
{
///<summary>
/// 得到当前显示数字的事件 需要在页面上触发
///</summary>

publicevent EventHandler onClick;

#region item
///<summary>
/// 当前的字数
///</summary>
privateint num;
///<summary>
/// 当前的数字
///</summary>
publicint Num
{
get { return num; }
set { num = value; }
}
#endregion

protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Lab_show.Text
="1";
HandControlVisible(
false, true);
}
}
protectedvoid But_low_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lab_show.Text);
if ((i -1) >1)
{
Lab_show.Text
= (i -1).ToString();
HandControlVisible(
true, true);
}
else
{
Lab_show.Text
= (i -1).ToString();
HandControlVisible(
false, true);
}
Click(sender, i
-1);
}
protectedvoid But_add_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lab_show.Text);
if ((i +1) <100)
{
Lab_show.Text
= (i +1).ToString();
HandControlVisible(
true, true);
}
else
{
Lab_show.Text
= (i +1).ToString();
HandControlVisible(
true, false);
}
Click(sender, i
+1);
}

//单击2个按钮都出发的事件 注册到页面上在处理
privatevoid Click(object sender, int i)
{
num
= i;
if (onClick !=null)
{
onClick(sender,
new EventArgs());
}
}

///<summary>
/// 设置按钮为可用或者不可用
///</summary>
///<param name="bl1"></param>
///<param name="bl2"></param>
publicvoid HandControlVisible(bool bl1, bool bl2)
{
But_low.Enabled
= bl1;
But_add.Enabled
= bl2;
}
}

 ——————————————————————————————————————————————-

页面代码

前台

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>
<%@ Register Src="~/ConputControl.ascx" TagName="ConputControl" TagPrefix="ascx"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style=" width:100%; height:300px; padding-left:27%;">
<br />

<table style=" width:500px; height:200px; background-color:#C49CD2">
<tr>
<td style="width:100%; height:100%" align="center" valign="middle">

<ascx:ConputControl ID="cc" runat="server" OnonClick="cc_Click"/>

<br />
<asp:Label ID="laber2" runat="server" Text="控件里面的内容是:"></asp:Label>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

 后台

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

publicpartialclass _Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
}
protectedvoid cc_Click(object sender, EventArgs e)
{
Label1.Text
= cc.Num.ToString();
}
}

 给我前辈能看了帮我解解惑?

posted @ 2011-02-14 17:02  北斗玄太虚  阅读(399)  评论(0编辑  收藏  举报