15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1218 ❘ ChaPTer 41 Asp.net feAtures<br />

Connecting Controls<br />

The connection between the Date <strong>and</strong> Events controls is achieved by passing an interface reference from<br />

DateSelectorControl to EventListControl (the two user control classes used by these Web Parts):<br />

public interface IDateProvider<br />

{<br />

SelectedDatesCollection SelectedDates<br />

{<br />

get;<br />

}<br />

}<br />

code snippet PCSWebParts/App_Code/IDateProvider.cs<br />

DateSelectorControl supports this interface, <strong>and</strong> so can pass an instance of IDateProvider by using<br />

this. The reference is passed by an endpoint method in DateSelectorControl, which is decorated with<br />

the ConnectionProvider attribute:<br />

[ConnectionProvider("Date Provider", "DateProvider")]<br />

public IDateProvider ProvideDate()<br />

{<br />

return this;<br />

}<br />

code snippet PCSWebParts/DateSelectorControl.ascx.cs<br />

This is all that is required to mark a Web Part as a provider control. You can then reference the provider by<br />

its endpoint ID, in this case DateProvider.<br />

To consume a provider, you use the ConnectionConsumer attribute to decorate a consumer method in<br />

EventListControl:<br />

[ConnectionConsumer("Date Consumer", "DateConsumer")]<br />

public void GetDate(IDateProvider provider)<br />

{<br />

this.provider = provider;<br />

IsConnected = true;<br />

SetDateLabel();<br />

}<br />

code snippet PCSWebParts/EventListControl.ascx.cs<br />

This method stores a reference to the IDateProvider interface passed, sets a flag, <strong>and</strong> changes the label text<br />

in the control.<br />

There is not a lot more to look at in this example. There are a few minor cosmetic sections of code, <strong>and</strong><br />

details for the event h<strong>and</strong>lers in Page_Load(), but nothing that you really need to see here. You can<br />

investigate further by examining the downloadable code for this chapter.<br />

There is, however, a whole lot more to Web Parts than this. The Web Parts framework is extremely<br />

powerful <strong>and</strong> richly featured. Whole books are devoted to the subject. Hopefully, though, this section has<br />

enabled you to get an insight into Web Parts <strong>and</strong> has demystified some of their functionality.<br />

asP.neT ajaX<br />

Web application programming is subject to continuous change <strong>and</strong> improvement. If you spend much<br />

time on the Internet, you may have noticed that more recent web sites are significantly better, in terms of<br />

usability, than older web sites. Many of today’s best web sites provide rich user interfaces that feel almost<br />

as responsive as Windows applications. They achieve this by using client-side processing, primarily through<br />

JavaScript code, <strong>and</strong> increasingly through a technology known as Ajax.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!