其实一开始我对于这个问题也很迷茫,经过几次“撞车后,以及请教优快云前辈后
将所学得的分享给有文之士:
用户控件代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="UserControl_WebUserControl" %>
<table style="width: 100%;">
<tr>
<td>
address1</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
address2</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
<td>
</td>
</tr>
</table>
在一个aspx页面中连续两次运用这个用户控件 要求在点击其中一个用户控件的Button后,将其中的TextBox值对应的显示到另外一个用户控件的对应的TextBox中
实现代码如下:
用户控件中代码如下:
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class UserControl_WebUserControl : System.Web.UI.UserControl
{
public EventHandler myClick;
public string Text1
{
set { TextBox1.Text = value; }
get { return TextBox1.Text; }
}
public string Text2
{
set { TextBox2.Text = value; }
get { return TextBox2.Text; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (myClick != null)
{
myClick(this, e);
}
}
}
aspx页面的实现代码如下:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1.myClick += new EventHandler(click1);
WebUserControl2.myClick += new EventHandler(click2);
}
protected void click1(Object sender, EventArgs e)
{
WebUserControl2.Text1 = WebUserControl1.Text1;
WebUserControl2.Text2 = WebUserControl1.Text2;
}
protected void click2(Object sender, EventArgs e)
{
Response.Write(WebUserControl2.Text1 + " " + WebUserControl2.Text2);
}
}
好了就是这样得了 看似很简单,也确实很简单
但有很多细节需要注意的
而我们普通的情况下 如在控制台应用程序下 实现委托和事件的方法可能如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public delegate void runing(string r,int key);
public class Program
{
public runing myclick;
static void Main(string[] args)
{
Program pa = new Program();
pa.getClick();
pa.myclick("Program", 1);
}
public void getClick()
{
if (myclick == null)
{
myclick += new runing(regText);
}
}
public void regText(string r, int key)
{
Console.WriteLine("我已被调用");
}
}
}
这样的用法 我想大家都不会他陌生
用户控件:

将所学得的分享给有文之士:
用户控件代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="UserControl_WebUserControl" %>
<table style="width: 100%;">
<tr>
<td>
address1</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
address2</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
<td>
</td>
</tr>
</table>
在一个aspx页面中连续两次运用这个用户控件 要求在点击其中一个用户控件的Button后,将其中的TextBox值对应的显示到另外一个用户控件的对应的TextBox中
实现代码如下:
用户控件中代码如下:
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class UserControl_WebUserControl : System.Web.UI.UserControl
{
public EventHandler myClick;
public string Text1
{
set { TextBox1.Text = value; }
get { return TextBox1.Text; }
}
public string Text2
{
set { TextBox2.Text = value; }
get { return TextBox2.Text; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (myClick != null)
{
myClick(this, e);
}
}
}
aspx页面的实现代码如下:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1.myClick += new EventHandler(click1);
WebUserControl2.myClick += new EventHandler(click2);
}
protected void click1(Object sender, EventArgs e)
{
WebUserControl2.Text1 = WebUserControl1.Text1;
WebUserControl2.Text2 = WebUserControl1.Text2;
}
protected void click2(Object sender, EventArgs e)
{
Response.Write(WebUserControl2.Text1 + " " + WebUserControl2.Text2);
}
}
好了就是这样得了 看似很简单,也确实很简单
但有很多细节需要注意的
而我们普通的情况下 如在控制台应用程序下 实现委托和事件的方法可能如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public delegate void runing(string r,int key);
public class Program
{
public runing myclick;
static void Main(string[] args)
{
Program pa = new Program();
pa.getClick();
pa.myclick("Program", 1);
}
public void getClick()
{
if (myclick == null)
{
myclick += new runing(regText);
}
}
public void regText(string r, int key)
{
Console.WriteLine("我已被调用");
}
}
}
这样的用法 我想大家都不会他陌生
用户控件:
本文介绍了一个用户控件的实现方式,通过两个用户控件之间的事件传递来更新文本框的内容。具体实现了点击一个用户控件中的按钮后,将该用户控件中的TextBox值复制到另一个用户控件的对应TextBox中。
148

被折叠的 条评论
为什么被折叠?



