About Web Part Communication

Source: Internet
Author: User

Web part is a new feature in Asp.net 2.0, through which end users can freely modify the page layout, the effect can look at Google Custom page http://www.google.com/ig? Hl = ZH-CN, which is also very simple to use. The following describes how to use two Web Part communications.

Web Part communication is managed by webpartmanager.
1. Define a public interface for communication.
2. Webpartmanager obtains the interface through the publisher.
3. Webpartmanager sends an interface to the subscriber.
I did an experiment on getting a date through the calendar control and passing it to textbox in another Web part, where something went wrong. First look at the interfaceCode

Using System;
Using System. Data;



Public   Interface Iselecteddate
{

Datetime selecteddate ();
}

As an excuse provider, calendar must first implement interfaces and be marked by [connectionprovider ("iselecteddate", "dateinfoprovider. In connectionprovider, the first parameter is the interface name, and the last parameter is the passed parameter. The Code is as follows:

Public Partial Class Calendaruc: system. Web. UI. usercontrol, iselecteddate
{
Datetime _ dateinfo = Datetime. now;
[Personalizable]
Public Datetime dateinfo
{
Get   {Return_ Dateinfo ;}
Set   {_ Dateinfo=Value ;}
}


[Connectionprovider ( " Iselecteddate " , " Dateinfoprovider " )]
Public Iselecteddate getdate ()
{
Return This;
}
Public Datetime selecteddate ()
{
Return_ Dateinfo;
}
Protected   Void Calendar1_selectionchanged ( Object Sender, eventargs E)
{
_ Dateinfo= This. Calendar1.selecteddate;

}



Protected   Void Page_load ( Object Sender, eventargs E)
{

}

}

Then the message subscriber should use the message, which must be marked by [connectionconsumer ("iselecteddate", "dateinfoconsumer. The Code is as follows:

Public Partial Class Google: system. Web. UI. usercontrol
{
Protected   Void Page_load ( Object Sender, eventargs E)
{

}
Protected   Void Button#click ( Object Sender, eventargs E)
{
Response. Write (page. isvalid );
StringQuerystr=Httputility. urlencode (textbox1.text );
Response. Redirect (@"Http://www.google.com/search? Q =" +Querystr );

}
[Connectionconsumer ( " Iselecteddate " , " Dateinfoconsumer " )]
Public   Void Getdate (iselecteddate searchtext)
{
Textbox1.text=Searchtext. selecteddate (). tostring ();

}
 
}

Finally, you can set the attributes of webpartmanager to establish a static connection. Modify the following code:

< ASP: webpartmanager ID = "Webpartmanager1" Runat = "Server" >
< Staticconnections >
< ASP: webpartconnection ID = "Dataconnection" Runat = "Server"
Providerid = "Calendaruc2" Providerconnectionpointid = "Dateinfoprovider" Consumerid = "Google1" Consumerconnectionpointid = "Dateinfoconsumer"   />
</ Staticconnections >

</ ASP: webpartmanager >

Finally, set the displaymode of webpartmanager to webpartmanager. connectdisplaymode;
Theoretically, if the above steps are completed, the establishment of static communication should be completed, but the following error will be reported after the operation.
The specified display mode is not supported on this page. Make sure
Personalization is enabled and the corresponding zones are present on
Page. The display mode can be set during and after page_init.
Parameter Name: Value

However, adding a connectionzone control on the page can solve this problem. However, after this operation, the end user will see the connectionzone control. There is always a better way to establish a connection completely through code, I hope you can tell me something about it.

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.