1.在我们写一个用户控件时需要考虑到重用得问题,如果控件中包含按钮事件,

我们不可能将点击事件写到控件里,而是我们想吧事件处理得过程写在

调用控件的页面中,这是该怎么处理呢?

我的做法时使用delegate来实现这个功能!

具体做法如下:
下面是控件的html部分
 1

控件的cs部分
 1委托在用户控件中的使用using System;
 2委托在用户控件中的使用using System.Data;
 3委托在用户控件中的使用using System.Configuration;
 4委托在用户控件中的使用using System.Collections;
 5委托在用户控件中的使用using System.Web;
 6委托在用户控件中的使用using System.Web.Security;
 7委托在用户控件中的使用using System.Web.UI;
 8委托在用户控件中的使用using System.Web.UI.WebControls;
 9委托在用户控件中的使用using System.Web.UI.WebControls.WebParts;
10委托在用户控件中的使用using System.Web.UI.HtmlControls;
11委托在用户控件中的使用
12委托在用户控件中的使用public partial class ctlForm : System.Web.UI.UserControl
13


我们调用这个控件的页面写法如下:
 1委托在用户控件中的使用<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testForm.aspx.cs" Inherits="testForm" %>
 2委托在用户控件中的使用
 3委托在用户控件中的使用<%@ Register Src="ctlForm.ascx" TagName="ctlForm" TagPrefix="uc1" %>
 4委托在用户控件中的使用
 5委托在用户控件中的使用<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 6委托在用户控件中的使用
 7委托在用户控件中的使用<html xmlns="http://www.w3.org/1999/xhtml" >
 8委托在用户控件中的使用<head runat="server">
 9委托在用户控件中的使用    <title>无标题页</title>
10委托在用户控件中的使用</head>
11委托在用户控件中的使用<body>
12委托在用户控件中的使用    <form id="form1" runat="server">
13委托在用户控件中的使用    <div>
14委托在用户控件中的使用        <uc1:ctlForm ID="CtlForm1" runat="server" />
15委托在用户控件中的使用    
16委托在用户控件中的使用    </div>
17委托在用户控件中的使用    </form>
18委托在用户控件中的使用</body>
19委托在用户控件中的使用</html>
20委托在用户控件中的使用
调用控件的cs代码如下

 1委托在用户控件中的使用using System;
 2委托在用户控件中的使用using System.Data;
 3委托在用户控件中的使用using System.Configuration;
 4委托在用户控件中的使用using System.Collections;
 5委托在用户控件中的使用using System.Web;
 6委托在用户控件中的使用using System.Web.Security;
 7委托在用户控件中的使用using System.Web.UI;
 8委托在用户控件中的使用using System.Web.UI.WebControls;
 9委托在用户控件中的使用using System.Web.UI.WebControls.WebParts;
10委托在用户控件中的使用using System.Web.UI.HtmlControls;
11委托在用户控件中的使用
12委托在用户控件中的使用public partial class testForm : System.Web.UI.Page
13
这样在控件点击按钮时,就会调用到页面的Test方法,从而实现了控件的重用.

相关文章: