ASP.NET 4 Unleased - p 24 ppsx

10 340 0
ASP.NET 4 Unleased - p 24 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

ptg 204 CHAPTER 4 Using the Rich Controls protected void calSchedule_DayRender(object sender, DayRenderEventArgs e) { if (schedule.FindRows(e.Day.Date).Length > 0) e.Cell.BackColor = System.Drawing.Color.Yellow; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Calendar Database</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Calendar id=”calSchedule” OnDayRender=”calSchedule_DayRender” Runat=”server” /> <br /> <asp:FormView id=”frmSchedule” AllowPaging=”True” DataKeyNames=”EntryDate” DataSourceID=”srcSchedule” Runat=”server”> <EmptyDataTemplate> <asp:LinkButton id=”btnNew” Text=”Add Entry” CommandName=”New” Runat=”server” /> </EmptyDataTemplate> <ItemTemplate> <h1><%# Eval(“EntryDate”, “{0:D}”) %></h1> <%# Eval(“Entry”) %> <br /><br /> <asp:LinkButton Id=”btnEdit” Text=”Edit Entry” CommandName=”Edit” Runat=”server” /> <asp:LinkButton Id=”lnkDelete” From the Library of Wow! eBook ptg 205 Displaying a Calendar 4 Text=”Delete Entry” CommandName=”Delete” OnClientClick=”return confirm(‘Delete entry?’);” Runat=”server” /> </ItemTemplate> <EditItemTemplate> <asp:Label id=”lblEntry” Text=”Entry:” AssociatedControlID=”txtEntry” Runat=”server” /> <br /> <asp:TextBox id=”txtEntry” Text=’<%#Bind(“Entry”) %>’ TextMode=”MultiLine” Columns=”40” Rows=”8” Runat=”server” /> <br /> <asp:LinkButton id=”btnUpdate” Text=”Update” CommandName=”Update” Runat=”server” /> </EditItemTemplate> <InsertItemTemplate> <asp:Label id=”lblEntry” Text=”Entry:” AssociatedControlID=”txtEntry” Runat=”server” /> <br /> <asp:TextBox id=”txtEntry” Text=’<%#Bind(“Entry”) %>’ TextMode=”MultiLine” Columns=”40” Rows=”8” Runat=”server” /> <br /> <asp:Button id=”btnInsert” Text=”Insert” CommandName=”Insert” Runat=”server” /> From the Library of Wow! eBook ptg 206 CHAPTER 4 Using the Rich Controls </InsertItemTemplate> </asp:FormView> <asp:SqlDataSource id=”srcSchedule” ConnectionString=”Server=.\SQLExpress;Integrated Security=True; AttachDbFileName=|DataDirectory|ScheduleDB.mdf;User Instance=True” SelectCommand=”SELECT EntryDate,Entry FROM Schedule WHERE ➥ EntryDate=@EntryDate” InsertCommand=”INSERT Schedule (EntryDate,Entry) VALUES ➥ (@EntryDate,@Entry)” UpdateCommand=”UPDATE Schedule SET Entry=@Entry WHERE EntryDate=@EntryDate” DELETECommand=”DELETE Schedule WHERE EntryDate=@EntryDate” Runat=”server”> <SelectParameters> <asp:ControlParameter Name=”EntryDate” ControlID=”calSchedule” PropertyName=”SelectedDate” /> </SelectParameters> <InsertParameters> <asp:ControlParameter Name=”EntryDate” ControlID=”calSchedule” PropertyName=”SelectedDate” /> </InsertParameters> </asp:SqlDataSource> <asp:SqlDataSource id=”srcCalendar” ConnectionString=”Server=.\SQLExpress;Integrated Security=True; AttachDbFileName=|DataDirectory|ScheduleDB.mdf;User Instance=True” SelectCommand=”SELECT EntryDate FROM Schedule” Runat=”server”> </asp:SqlDataSource> </div> </form> </body> </html> The page in Listing 4.10 saves and loads entries from a SQL Express database named ScheduleDB. The contents of the schedule are contained in a table named Schedule that has the following schema: From the Library of Wow! eBook ptg 207 Displaying Advertisements 4 The tricky part in Listing 4.10 is the code for highlighting the current entries in the calen- dar. In the Page_PreRender event handler, a list of all the current entries is retrieved from the database. The list is represented by a DataView object. The DayRender event is raised when the Calendar renders each day (table cell). In the DayRender event handler in Listing 4.10, if an entry is in the database that corresponds to the day being rendered, the day is highlighted with a yellow background color. Displaying Advertisements The AdRotator control enables you to randomly display different advertisements in a page. You can store the list of advertisements in either an XML file or in a database table. The AdRotator control supports the following properties (this is not a complete list): . AdvertisementFile—Enables you to specify the path to an XML file that contains a list of banner advertisements. . AlternateTextField—Enables you to specify the name of the field for displaying alternate text for the banner advertisement image. The default value is AlternateText. . DataMember—Enables you to bind to a particular data member in the data source. . DataSource—Enables you to specify a data source programmatically for the list of banner advertisements. . DataSourceID—Enables you to bind to a data source declaratively. . ImageUrlField—Enables you to specify the name of the field for the image URL for the banner advertisement. The default value for this field is ImageUrl. . KeywordFilter—Enables you to filter advertisements by a single keyword. . NavigateUrlField—Enables you to specify the name of the field for the advertise- ment link. The default value for this field is NavigateUrl. . Target—Enables you to open a new window when a user clicks the banner advertisement. The AdRotator control also supports the following event: . AdCreated—Raised after the AdRotator control selects an advertisement but before the AdRotator control renders the advertisement. Column Name Data Type EntryDate DateTime Entry Nvarchar(max) From the Library of Wow! eBook ptg 208 CHAPTER 4 Using the Rich Controls The AdRotator control includes a KeywordFilter property. You can provide each banner advertisement with a keyword and then filter the advertisements displayed by the AdRotator control by using the value of the KeywordFilter property. This property can be used in multiple ways. For example, if you display more than one advertisement in the same page, you can filter the advertisements by page regions. You can use the KeywordFilter to show the big banner advertisement on the top of the page and box ads on the side of the page. You can also use the KeywordFilter property to filter advertisements by website section. For example, you might want to show different advertisements on your website’s home page than on your website’s search page. NOTE If you cache a page that contains an AdRotator control, the AdRotator control is excluded from the cache. In other words, even if you cache a page, randomly selected banner advertisements still display. The AdRotator control takes advantage of a fea- ture of the ASP.NET Framework called post-cache substitution. You learn more about this feature in Chapter 29, “Caching Application Pages and Data.” Storing Advertisements in an XML File You can store the list of advertisements that the AdRotator displays in an XML file by setting the AdRotator control’s AdvertisementFile property. For example, the page in Listing 4.11 contains three AdRotator controls that retrieve banner advertisements from an XML file named AdList.xml (see Figure 4.8). LISTING 4.11 AdRotatorXML.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <style type=”text/css”> html { background-color:silver; } .content { background-color:white; padding:10px; border:solid 1px black; margin:auto; width:400px; From the Library of Wow! eBook ptg 209 Displaying Advertisements 4 text-align:center; } .box { float:right; padding:10px; border-left:solid 1px black; } .clear { clear:both; } </style> <title>AdRotator XML</title> </head> <body> <form id=”form1” runat=”server”> <div class=”content”> <asp:AdRotator id=”AdRotator1” AdvertisementFile=”~/App_Data/AdList.xml” KeywordFilter=”banner” CssClass=”banner” Runat=”server” /> <br /> <div class=”box”> <asp:AdRotator id=”AdRotator2” AdvertisementFile=”~/App_Data/AdList.xml” KeywordFilter=”box” Runat=”server” /> <br /><br /> <asp:AdRotator id=”AdRotator3” AdvertisementFile=”~/App_Data/AdList.xml” KeywordFilter=”box” Runat=”server” /> </div> <br />Here is the body text in the page. <br />Here is the body text in the page. <br />Here is the body text in the page. From the Library of Wow! eBook ptg 210 CHAPTER 4 Using the Rich Controls <br />Here is the body text in the page. <br class=”clear” /> </div> </form> </body> </html> FIGURE 4.8 Displaying advertisements from an XML file. The page in Listing 4.11 contains an AdRotator control that displays a banner advertise- ment at the top of the page. The page also contains two AdRotator controls that display box advertisements on the right of the page. The first AdRotator has a KeyworldFilter property that has the value banner, and the remaining two AdRotator controls have KeywordFilter properties with the value box. The first AdRotator displays only banner advertisements, and the remaining two AdRotator controls display only box advertisements. All three AdRotator controls get their list of banner advertisements from a file named AdList.xml. This file is located in the App_Data folder for security reasons. The files in the App_Data folder cannot be opened in a web browser. From the Library of Wow! eBook ptg 211 Displaying Advertisements 4 NOTE There is nothing wrong with assigning different XML files to different AdRotator con- trols. For example, you could create distinct BannerAd.xml and BoxAd.xml files, and then you would not have to worry about the KeywordFilter property. The file in Listing 4.12 contains the contents of the AdList.xml file. LISTING 4.12 AdList.xml <?xml version=”1.0” encoding=”utf-8” ?> <Advertisements> <! Banner Advertisements > <Ad> <ImageUrl>~/Ads/BannerAd1.gif</ImageUrl> <Width>300</Width> <Height>50</Height> <NavigateUrl>http://www.AspWorkshops.com</NavigateUrl> <AlternateText>Banner Advertisement 1</AlternateText> <Impressions>50</Impressions> <Keyword>banner</Keyword> </Ad> <Ad> <ImageUrl>~/Ads/BannerAd2.gif</ImageUrl> <Width>300</Width> <Height>50</Height> <NavigateUrl>http://www.AspWorkshops.com</NavigateUrl> <AlternateText>Banner Advertisement 2</AlternateText> <Impressions>25</Impressions> <Keyword>banner</Keyword> </Ad> <Ad> <ImageUrl>~/Ads/BannerAd3.gif</ImageUrl> <Width>300</Width> <Height>50</Height> <NavigateUrl>http://www.AspWorkshops.com</NavigateUrl> <AlternateText>Banner Advertisement 3</AlternateText> <Impressions>25</Impressions> <Keyword>banner</Keyword> </Ad> <! Box Advertisements > <Ad> <ImageUrl>~/Ads/BoxAd1.gif</ImageUrl> <Width>150</Width> From the Library of Wow! eBook ptg 212 CHAPTER 4 Using the Rich Controls <Height>150</Height> <NavigateUrl>http://www.AspWorkshops.com</NavigateUrl> <AlternateText>Box Advertisement 1</AlternateText> <Impressions>50</Impressions> <Keyword>box</Keyword> </Ad> <Ad> <ImageUrl>~/Ads/BoxAd2.gif</ImageUrl> <Width>150</Width> <Height>150</Height> <NavigateUrl>http://www.AspWorkshops.com</NavigateUrl> <AlternateText>Box Advertisement 2</AlternateText> <Impressions>50</Impressions> <Keyword>box</Keyword> </Ad> </Advertisements> The Impressions attribute in the file in Listing 4.12 determines how often each banner advertisement displays. For example, the first banner advertisement displays 50% of the time, and the remaining two banner advertisements display 25% of the time. Storing Advertisements in a Database Table Rather than store the list of advertisements in an XML file, you can store the list in a data- base table. For example, the AdRotator control contained in Listing 4.13 is bound to a SqlDataSource control. The SqlDataSource control represents the contents of a database table named AdList, which is located in a SQL Express database named AdListDB. LISTING 4.13 AdRotatorDatabase.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>AdRotator Database</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:AdRotator id=”AdRotator1” DataSourceID=”srcAds” From the Library of Wow! eBook ptg 213 Displaying Advertisements 4 Runat=”server” /> <asp:SqlDataSource id=”srcAds” ConnectionString=”Server=.\SQLExpress;Integrated Security=True; AttachDbFileName=|DataDirectory|AdListDB.mdf;User Instance=True” SelectCommand=”SELECT ImageUrl, Width, Height, NavigateUrl, AlternateText, Keyword, Impressions FROM AdList” Runat=”server” /> </div> </form> </body> </html> To use the page in Listing 4.13, you need to create the AdList database table. This table has the following schema: Column Name Data Type Id Int (IDENTITY) ImageUrl Varchar(250) Width Int Height Int NavigateUrl Varchar(250) AlternateText NVarchar(100) Keyword NVarchar(50) Impressions Int The columns in the AdList database table correspond to the attributes in the AdList.xml file discussed in the previous section. Tracking Impressions and Transfers Normally, when you display advertisements, you do it to make money. Your advertisers want statistics on how often their advertisements display (the number of impressions) and how often their advertisements are clicked (the number of transfers). To track the number of times that an advertisement displays, you need to handle the AdRotator control’s AdCreated event. To track the number of times that an advertisement is clicked, you need to create a redirect handler. From the Library of Wow! eBook . border:solid 1px black; margin:auto; width :40 0px; From the Library of Wow! eBook ptg 209 Displaying Advertisements 4 text-align:center; } .box { float:right; padding:10px; border-left:solid 1px. Figure 4. 8). LISTING 4. 11 AdRotatorXML.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>. control takes advantage of a fea- ture of the ASP. NET Framework called post-cache substitution. You learn more about this feature in Chapter 29, “Caching Application Pages and Data.” Storing Advertisements

Ngày đăng: 06/07/2014, 18:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan