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 interface code
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 );
String querystr = 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.