Implementation of ASP. NET UserControl Communication

Source: Internet
Author: User
Tags tagname

Recently, the WebPart communication technology was used in SharePoint2007. I personally think that the ConnectionConsumer and ConnectionProvider in version 2007 are not as easy as those in version 2010, so I changed the idea to implement communication: A UserControl control is installed in the WebPart container, so that as long as the UserControl can communicate, the communication between webparts is realized.

UserControl is a user-defined control. We can inject events into UserControl. When a UserControl triggers an event, data is transmitted through event parameters, allow other UserControl to obtain the parameters passed by this event for communication.

The following is a simple demonstration of two usercontrols for communication.

Create a class, two usercontrols and one web page.

Here are MyEventAgrs. cs, UCProvider. ascx, UCComsumer. ascx, Default. aspx.

The MyEventAgrs. cs code is as follows:

Copy codeThe Code is as follows: 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 ;}
}

If you want to pass other objects, you only need to modify the MyMsg method of the MyEventAgrs class. The EventArgs abstract class must be inherited to store event parameter values. In addition, a delegate event must be defined and used elsewhere.

The UCProvider. ascx code is as follows:

Copy codeThe Code is as follows: 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 (dropdownlist+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 (dropdownlist+selectedindexchanged );
}
Void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
MyEventAgrs myEvent = new MyEventAgrs ();
MyEvent. MyMsg = DropDownList1.SelectedValue;
MyHandle (this, myEvent );
}}

A DropDownList is defined on the foreground page and a data source is bound to the DropDownList. I want to transmit the value of the user-selected DropDownList when selecting DropDownList. The Code uses myHandle (this, myEvent) to initialize the event.

The UCComsumer. ascx code is as follows:

Copy codeThe Code is as follows: 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;
}
}

Attribute representation can also be used when values are assigned here, for example:Copy codeThe Code is as follows: 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 ;}
}

This method makes it easier to reference UserControl:Copy codeThe Code is as follows: <uc2: ucConsumer runat = "server" ID = "uc2" UC1Msg = "Defalut Value"/>

<Uc2: ucConsumer runat = "server" ID = "uc2" UC1Msg = "Defalut Value"/>

The Default. aspx code is as follows:

Register UserControl on the front-end page

Copy codeThe Code is as follows: <% @ 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 "%>

Reference again

Copy codeThe Code is as follows: <uc1: ucProvider runat = "server" ID = "uc1" OnmyHandle = "ucloud myhandle"/>
<Uc2: ucConsumer runat = "server" ID = "uc2"/>

<Uc1: ucProvider runat = "server" ID = "uc1" OnmyHandle = "ucloud myhandle"/>
<Uc2: ucConsumer runat = "server" ID = "uc2"/>

Background page:Copy codeThe Code is as follows: protected void ucloud myhandle (object sender, MyEventAgrs args)
{
If (args! = Null)
{
Uc2.InitValue (args. MyMsg );
}
Else
Uc2.UC1Msg = string. Empty;
}

Protected void ucloud myhandle (object sender, MyEventAgrs args)
{
If (args! = Null)
{
Uc2.InitValue (args. MyMsg );
}
Else
Uc2.UC1Msg = string. Empty;
}

This completes the entire process. The Default. aspx page is only a carrier or intermediate medium. All operations are performed between two usercontrols. However, when the page is loaded for the first time, that is, when the page is loaded too many drop-down boxes are not clicked, there is no value transfer here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.