ASP.NET UserControl 通信
最近在SharePoint2007中用到了WebPart通信技术,个人觉得2007版本里面的ConnectionConsumer和ConnectionProvider没有2010版本那么好用,于是换了一种思想去实现通信:WebPart容器中装一个UserControl控件,这样只要UserControl可以通信,就实现了WebPart之间通信。
下面我就用ASP.NET的UserControl模拟SharePoint UserControl通信,两者的本质,思想和实现方式都不变。
UserControl是用户自定义控件,我们可以在UserControl中注入事件,当一个UserControl触发某种事件,然后通过事件参数来传递数据,让其他UserControl获得这个事件传递过来的参数,实现通信。
下面简单演示一下两个UserControl进行通信
创建好一个类,两个UserControl和一个web页面。
这里分别是MyEventAgrs.cs,UCProvider.ascx,UCComsumer.ascx,Default.aspx。
MyEventAgrs.cs代码如下:
[csharp] public delegate void MyEventHandle(object sender, MyEventAgrs args);
public class MyEventAgrs : EventArgs
{
public MyEventAgrs() { }
public string MyMsg { get; set; }
}
public delegate void MyEventHandle(object sender, MyEventAgrs args);
public class MyEventAgrs : EventArgs
{
public MyEventAgrs() { }
public string MyMsg { get; set; }
}
如果想传递其他对象时就只需要修改MyEventAgrs类的MyMsg方法。这里须继承EventArgs抽象类,用于存放事件参数值,另外需定义一个委托事件,然后在其他的地方使用。
UCProvider.ascx代码如下:
[csharp] public partial class UCProvider : System.Web.UI.UserControl
{
public event MyEventHandle myHandle;
protected void Page_Load(object sender, EventArgs e)
{
this.DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
}
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
MyEventAgrs myEvent = new MyEventAgrs();
myEvent.MyMsg = DropDownList1.SelectedValue;
myHandle(this, myEvent);
} }
public partial class UCProvider : System.Web.UI.UserControl
{
public event MyEventHandle myHandle;
protected void Page_Load(object sender, EventArgs e)
{
this.DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
}
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
MyEventAgrs myEvent = new MyEventAgrs();
myEvent.MyMsg = DropDownList1.SelectedValue;
myHandle(this, myEvent);
} }
这里在前台页面中定义了一个DropDownList,并给DropDownList绑定了数据源,不再罗列前台页面。我想实现当用户选择DropDownList的时候将用户选择的DropDownList的值传递出去,代码里面用myHandle(this, myEvent)来初始化事件。
UCComsumer.ascx代码如下:
[csharp] public partial class UCComsumer : System.Web.UI.UserControl
{
public void InitValue(string msg) {
lb.Text = msg;
}
}
public partial class UCComsumer : System.Web.UI.UserControl
{
public void InitValue(string msg) {
lb.Text = msg;
}
}
这里赋值的时候也可以用属性来表示,比如:
[csharp] public string UC1Msg
{
get{return this.lb.Text;}
set{this.lb.Text=value;}
}
public string UC1Msg
{
get{return this.lb.Text;}
set{this.lb.Text=value;}
}
如果用这种方式,在引用UserControl的时候就更加方便了:
[csharp] <uc2:ucConsumer runat="server" ID="uc2" UC1Msg="De易做图t Value" />
<uc2:ucConsumer runat="server" ID="uc2" UC1Msg="De易做图t Value" />Default.aspx代码如下:
前台页面里面需要注册UserControl
[csharp] <%@ Register TagPrefix="uc1" TagName="ucProvider" Src="~/UserControls/UCProvider.ascx" %>
<%@ Register TagPrefix="uc2" TagName="ucConsumer" Src="~/UserControls/UCComsumer.ascx" %>
<%@ Register TagPrefix="uc1" TagName="ucProvider" Src="~/UserControls/UCProvider.ascx" %>
<%@ Register TagPrefix="uc2" TagName="ucConsumer" Src="~/UserControls/UCComsumer.ascx" %>
再引用
[javascript] <uc1:ucProvider runat="server" ID="uc1" OnmyHandle="uc1_myHandle" />
<uc2:ucConsumer runat="server" ID="uc2" />
<uc1:ucProvider runat="server" ID="uc1" OnmyHandle="uc1_myHandle" />
<uc2:ucConsumer runat="server" ID="uc2" />
后台页面:
[csharp] protected void uc1_myHandle(object sender, MyEventAgrs args)
{
if (args != null)
{
uc2.InitValue(args.MyMsg);
}
else
uc2.UC1Msg = string.Empty;
}
protected void uc1_myHandle(object sender, MyEventAgrs args)
{
&nb
补充:Web开发 , ASP.Net ,