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

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

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

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

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

控件的cs部分
 1用户控件事件使用delegateusing System;
 2用户控件事件使用delegateusing System.Data;
 3用户控件事件使用delegateusing System.Configuration;
 4用户控件事件使用delegateusing System.Collections;
 5用户控件事件使用delegateusing System.Web;
 6用户控件事件使用delegateusing System.Web.Security;
 7用户控件事件使用delegateusing System.Web.UI;
 8用户控件事件使用delegateusing System.Web.UI.WebControls;
 9用户控件事件使用delegateusing System.Web.UI.WebControls.WebParts;
10用户控件事件使用delegateusing System.Web.UI.HtmlControls;
11用户控件事件使用delegate
12用户控件事件使用delegatepublic partial class ctlForm : System.Web.UI.UserControl
13


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

 1用户控件事件使用delegateusing System;
 2用户控件事件使用delegateusing System.Data;
 3用户控件事件使用delegateusing System.Configuration;
 4用户控件事件使用delegateusing System.Collections;
 5用户控件事件使用delegateusing System.Web;
 6用户控件事件使用delegateusing System.Web.Security;
 7用户控件事件使用delegateusing System.Web.UI;
 8用户控件事件使用delegateusing System.Web.UI.WebControls;
 9用户控件事件使用delegateusing System.Web.UI.WebControls.WebParts;
10用户控件事件使用delegateusing System.Web.UI.HtmlControls;
11用户控件事件使用delegate
12用户控件事件使用delegatepublic partial class testForm : System.Web.UI.Page
13

相关文章:

  • 2021-05-05
  • 2021-07-16
  • 2021-07-24
  • 2022-12-23
  • 2021-08-04
猜你喜欢
  • 2022-02-10
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2021-07-19
相关资源
相似解决方案