asp.net UserControl Communication Implementation _ practical skills

Source: Internet
Author: User
Tags tagname

Recently in SharePoint2007 used WebPart communication technology, personally feel the 2007 version of ConnectionConsumer and ConnectionProvider no 2010 version so good, So change a thought to realize communication: The WebPart container is loaded with a UserControl control so that as long as the UserControl can communicate, the communication between WebPart is achieved.

UserControl is a user-defined control where we can inject events into the UserControl, when a UserControl triggers an event, and then passes the data through the event arguments, allowing the other UserControl to obtain the arguments passed over by the event to achieve communication.

Here's a quick demo of two UserControl to communicate

Create a class, two UserControl, and a Web page.

This is myeventagrs.cs,ucprovider.ascx,uccomsumer.ascx,default.aspx.

The MyEventAgrs.cs code is as follows:

Copy Code code 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. You must inherit the EventArgs abstract class to hold the event parameter values, and you need to define a delegate event and use it elsewhere.

The Ucprovider.ascx code is as follows:

Copy Code code 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 (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);
}    }


Here, a DropDownList is defined in the foreground page, and a data source is bound to the DropDownList, and the foreground page is no longer listed. I want to implement the DropDownList value of the user's choice when the user chooses DropDownList, and the code uses Myhandle (this, myevent) to initialize the event.

The Uccomsumer.ascx code is as follows:

Copy Code code 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;
}
}


You can also use attributes when assigning values, such as:
Copy Code code 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;}
}


In this way, it is more convenient to quote UserControl:
Copy Code code 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:

The front page needs to be registered UserControl

Copy Code code 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"%>


Re-reference

Copy Code code as follows:

<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"/>


Background page:
Copy Code code as follows:

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)
{
if (args!= null)
{
Uc2. InitValue (args. MYMSG);
}
Else
Uc2. Uc1msg = string. Empty;
}


This completes the whole process. The Default.aspx page is only a carrier or intermediate medium, and all operations are performed between two UserControl. But when the page is first loaded, that is, the page is loaded too far down the dropdown box has not been clicked, here is no value to pass.

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.