ptg 194 CHAPTER 4 Using the Rich Controls LISTING 4.6 FileHandlerLarge.ashx <%@ WebHandler Language=”C#” Class=”FileHandlerLarge” %> using System; using System.Web; using System.Data; using System.Data.SqlClient; public class FileHandlerLarge : IHttpHandler { const string conString = @”Server=.\SQLExpress;Integrated Security=True; AttachDbFileName=|DataDirectory|FilesDB.mdf;User Instance=True”; public void ProcessRequest (HttpContext context) { context.Response.Buffer = false; context.Response.ContentType = “application/msword”; SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand(“SELECT FileBytes FROM Files WHERE ➥ Id=@Id”, con); cmd.Parameters.AddWithValue(“@Id”, context.Request[“Id”]); using (con) { con.Open(); SqlDataReader reader = cmd.ExecuteReader ➥ (CommandBehavior.SequentialAccess); if (reader.Read()) { int bufferSize = 8040; byte[] chunk = new byte[bufferSize]; long retCount; long startIndex = 0; retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize); while (retCount == bufferSize) { context.Response.BinaryWrite(chunk); startIndex += bufferSize; retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize); From the Library of Wow! eBook ptg 195 Displaying a Calendar 4 } byte[] actualChunk = new Byte[retCount - 1]; Buffer.BlockCopy(chunk, 0, actualChunk, 0, (int)retCount - 1); context.Response.BinaryWrite(actualChunk); } } } public bool IsReusable { get { return false; } } } The HTTP Handler in Listing 4.6 uses a SqlDataReader to retrieve a file from the database. The SqlDataReader is retrieved with a CommandBehavior.SequentialAccess parameter that enables the SqlDataReader to load data as a stream. The contents of the database column are pulled into memory in 8,040-byte chunks. The chunks are written to the browser with the Response.BinaryWrite() method. Response buffering is disabled for the handler. The Response.Buffer property is set to the value False. Because buffering is disabled, the output of the handler is not buffered in server memory before being transmitted to the browser. WARNING The method of working with large files described in this section only works with SQL Server 2005 and SQL Server 2008. When using earlier versions of SQL Server, you need to use the TEXTPTR() function instead of the .WRITE clause. Displaying a Calendar The Calendar control enables you to display a calendar. You can use the calendar as a date picker or to display a list of upcoming events. The page in Listing 4.7 displays a simple calendar with the Calendar control (see Figure 4.4). From the Library of Wow! eBook ptg 196 CHAPTER 4 Using the Rich Controls LISTING 4.7 ShowCalendar.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>Show Calendar</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Calendar id=”Calendar1” Runat=”server” /> </div> </form> </body> </html> FIGURE 4.4 Displaying a calendar with the Calendar control. From the Library of Wow! eBook ptg 197 Displaying a Calendar 4 The Calendar control supports the following properties (this is not a complete list): . DayNameFormat—Enables you to specify the appearance of the days of the week. Possible values are FirstLetter, FirstTwoLetters, Full, Short, and Shortest. . NextMonthText—Enables you to specify the text that appears for the next month link. . NextPrevFormat—Enables you to specify the format of the next month and previous month link. Possible values are CustomText, FullMonth, and ShortMonth. . PrevMonthText—Enables you to specify the text that appears for the previous month link. . SelectedDate—Enables you to get or set the selected date. . SelectedDates—Enables you to get or set a collection of selected dates. . SelectionMode—Enables you to specify how dates are selected. Possible values are Day, DayWeek, DayWeekMonth, and None. . SelectMonthText—Enables you to specify the text that appears for selecting a month. . SelectWeekText—Enables you to specify the text that appears for selecting a week. . ShowDayHeader—Enables you to hide or display the day names at the top of the Calendar control. . ShowNextPrevMonth—Enables you to hide or display the links for the next and previ- ous months. . ShowTitle—Enables you to hide or display the title bar displayed at the top of the calendar. . TitleFormat—Enables you to format the title bar. Possible values are Month and MonthYear. . TodaysDate—Enables you to specify the current date. This property defaults to the current date on the server. . VisibleDate—Enables you to specify the month displayed by the Calendar control. This property defaults to displaying the month that contains the date specified by TodaysDate. The Calendar control also supports the following events: . DayRender—Raised as each day is rendered. . SelectionChanged—Raised when a new day, week, or month is selected. . VisibleMonthChanged—Raised when the next or previous month link is clicked. From the Library of Wow! eBook ptg 198 CHAPTER 4 Using the Rich Controls The SelectionMode property enables you to change the behavior of the calendar so that you can not only select days, but also select weeks or months. The page in Listing 4.8 illustrates how you can use the SelectionMode property with the SelectedDates property to select multiple dates (see Figure 4.5). LISTING 4.8 CalendarSelectionMode.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <script runat=”server”> protected void btnSubmit_Click(object sender, EventArgs e) { bltResults.DataSource = Calendar1.SelectedDates; bltResults.DataBind(); } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Calendar SelectionMode</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Calendar id=”Calendar1” SelectionMode=”DayWeekMonth” runat=”server” /> <br /><br /> <asp:Button id=”btnSubmit” Text=”Submit” OnClick=”btnSubmit_Click” Runat=”server” /> <hr /> <asp:BulletedList id=”bltResults” DataTextFormatString=”{0:d}” Runat=”server” /> From the Library of Wow! eBook ptg 199 Displaying a Calendar 4 </div> </form> </body> </html> FIGURE 4.5 Selecting weeks and months with a Calendar control. When you select a date, or group of dates, from the Calendar control in Listing 4.8, the set of selected dates display in a BulletedList control. Creating a Pop-Up Date Picker You can use a Calendar control to create a fancy pop-up date picker if you are willing to add a little JavaScript and some Cascading Style Sheet (CSS) rules to a page. The page in Listing 4.9 contains a TextBox and Calendar control (see Figure 4.6). The Calendar control is hidden until you click the calendar image. The #datePicker style sheet rules sets the display property to none. When you click the image of the calendar, the JavaScript displayCalendar() function executes and sets the CSS display property to the value block. When you select a date from the calendar, the page is posted back to the server and the SelectionChanged server-side event is raised. The SelectionChanged event handler updates the TextBox control with the selected date. From the Library of Wow! eBook ptg 200 CHAPTER 4 Using the Rich Controls LISTING 4.9 CalendarJS.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <script runat=”server”> protected void calEventDate_SelectionChanged(object sender, EventArgs e) { txtEventDate.Text = calEventDate.SelectedDate.ToString(“d”); } protected void btnSubmit_Click(object sender, EventArgs e) { lblResult.Text = “You picked: “ + txtEventDate.Text; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <script type=”text/javascript”> function displayCalendar() { var datePicker = document.getElementById(‘datePicker’); datePicker.style.display = ‘block’; } </script> <style type=”text/css”> #datePicker { display:none; position:absolute; border:solid 2px black; background-color:white; } .content { width:400px; background-color:white; margin:auto; padding:10px; } html { From the Library of Wow! eBook ptg 201 Displaying a Calendar 4 background-color:silver; } </style> <title>Calendar with JavaScript</title> </head> <body> <form id=”form1” runat=”server”> <div class=”content”> <asp:Label id=”lblEventDate” Text=”Event Date:” AssociatedControlID=”txtEventDate” Runat=”server” /> <asp:TextBox id=”txtEventDate” Runat=”server” /> <img src=”Calendar.gif” onclick=”displayCalendar()” /> <div id=”datePicker”> <asp:Calendar id=”calEventDate” OnSelectionChanged=”calEventDate_SelectionChanged” Runat=”server” /> </div> <br /> <asp:Button id=”btnSubmit” Text=”Submit” Runat=”server” OnClick=”btnSubmit_Click” /> <hr /> <asp:Label id=”lblResult” Runat=”server” /> </div> </form> </body> </html> From the Library of Wow! eBook ptg 202 CHAPTER 4 Using the Rich Controls Rendering a Calendar from a Database Table You also can use the Calendar control to display events in a calendar. In this section, we build a simple schedule application that enables you to insert, update, and delete calendar entries. Each schedule entry is highlighted in a Calendar control (see Figure 4.7). The code for the schedule application is contained in Listing 4.10. FIGURE 4.6 Displaying a pop-up calendar. From the Library of Wow! eBook ptg 203 Displaying a Calendar 4 LISTING 4.10 CalendarDatabase.aspx <%@ Page Language=”C#” ValidateRequest=”false” %> <%@ Import Namespace=”System.Data” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <script runat=”server”> DataView schedule = new DataView(); void Page_Load() { if (calSchedule.SelectedDate == DateTime.MinValue) calSchedule.SelectedDate = calSchedule.TodaysDate; } void Page_PreRender() { schedule = (DataView)srcCalendar.Select(DataSourceSelectArguments.Empty); schedule.Sort = “EntryDate”; } FIGURE 4.7 Displaying a calendar from a database. From the Library of Wow! eBook . contained in Listing 4. 10. FIGURE 4. 6 Displaying a pop-up calendar. From the Library of Wow! eBook ptg 203 Displaying a Calendar 4 LISTING 4. 10 CalendarDatabase.aspx <%@ Page Language=”C#”. Wow! eBook ptg 200 CHAPTER 4 Using the Rich Controls LISTING 4. 9 CalendarJS.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. Creating a Pop-Up Date Picker You can use a Calendar control to create a fancy pop-up date picker if you are willing to add a little JavaScript and some Cascading Style Sheet (CSS) rules to a page.