1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 23 doc

10 242 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 582,77 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

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);

Trang 2

}

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)

Trang 3

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>

Trang 4

The Calendar control supports the following properties (this is not a complete list):

Possible values are FirstLetter, FirstTwoLetters, Full, Short, and Shortest

link

month link Possible values are CustomText, FullMonth, and ShortMonth

link

DayWeek, DayWeekMonth, and None

month

Calendar control

previ-ous months

calendar

MonthYear

current date on the server

This property defaults to displaying the month that contains the date specified by

TodaysDate

The Calendar control also supports the following events:

Trang 5

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}”

Trang 6

</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

Trang 7

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;

}

Trang 8

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>

Trang 9

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

Trang 10

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

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

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN