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

ASP.NET 4 Unleased - p 67 pps

10 200 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 512,44 KB

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

Nội dung

LISTING 13.5 EditRepeater.aspx // The name of the primary key column string DataKeyName = “Id”; /// /// Stores the primary keys in ViewState /// Hashtable Keys { get { if ViewState

Trang 1

LISTING 13.5 EditRepeater.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

// The name of the primary key column

string DataKeyName = “Id”;

/// <summary>

/// Stores the primary keys in ViewState

/// </summary>

Hashtable Keys

{

get

{

if (ViewState[“Keys”] == null)

ViewState[“Keys”] = new Hashtable();

return (Hashtable)ViewState[“Keys”];

}

}

FIGURE 13.4 Editing database records with the Repeater control

Trang 2

/// <summary>

/// Build the primary key collection

/// </summary>

protected void rptMovies_ItemDataBound(object sender, RepeaterItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==

ListItemType.AlternatingItem)

{

Keys.Add(e.Item.ItemIndex, DataBinder.Eval(e.Item.DataItem, “Id”));

}

}

/// <summary>

/// Clear the primary keys when Repeater is rebound

/// to its data source

/// </summary>

protected void rptMovies_DataBinding(object sender, EventArgs e)

{

Keys.Clear();

}

/// <summary>

/// When you click the Update,Insert, or Delete

/// button, this method executes

/// </summary>

protected void rptMovies_ItemCommand(object source, RepeaterCommandEventArgs e)

{

switch (e.CommandName)

{

case “Update”:

UpdateMovie(e);

break;

case “Insert”:

InsertMovie(e);

break;

case “Delete”:

DeleteMovie(e);

break;

}

}

/// <summary>

/// Update a movie record

/// </summary>

void UpdateMovie(RepeaterCommandEventArgs e)

Trang 3

{

// Get the form fields

TextBox txtTitle = (TextBox)e.Item.FindControl(“txtTitle”);

TextBox txtDirector = (TextBox)e.Item.FindControl(“txtDirector”);

CheckBox chkInTheaters = (CheckBox)e.Item.FindControl(“chkInTheaters”);

// Set the DataSource parameters

srcMovies.UpdateParameters[“Id”].DefaultValue =

Keys[e.Item.ItemIndex].ToString();

srcMovies.UpdateParameters[“Title”].DefaultValue = txtTitle.Text;

srcMovies.UpdateParameters[“Director”].DefaultValue = txtDirector.Text;

srcMovies.UpdateParameters[“InTheaters”].DefaultValue =

chkInTheaters.Checked.ToString();

// Fire the UpdateCommand

srcMovies.Update();

}

/// <summary>

/// Insert a movie record

/// </summary>

void InsertMovie(RepeaterCommandEventArgs e)

{

// Get the form fields

TextBox txtTitle = (TextBox)e.Item.FindControl(“txtTitle”);

TextBox txtDirector = (TextBox)e.Item.FindControl(“txtDirector”);

CheckBox chkInTheaters = (CheckBox)e.Item.FindControl(“chkInTheaters”);

// Set the DataSource parameters

srcMovies.InsertParameters[“Title”].DefaultValue = txtTitle.Text;

srcMovies.InsertParameters[“Director”].DefaultValue = txtDirector.Text;

srcMovies.InsertParameters[“InTheaters”].DefaultValue =

chkInTheaters.Checked.ToString();

// Fire the InsertCommand

srcMovies.Insert();

}

/// <summary>

/// Delete a movie record

/// </summary>

void DeleteMovie(RepeaterCommandEventArgs e)

{

// Set the DataSource parameters

srcMovies.DeleteParameters[“Id”].DefaultValue =

Keys[e.Item.ItemIndex].ToString();

Trang 4

// Fire the DeleteCommand

srcMovies.Delete();

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<style type=”text/css”>

html

{

background-color:silver;

}

.content

{

width:600px;

height:400px;

padding:10px;

border:solid 1px black;

background-color:white;

}

.movies td

{

text-align:center;

}

a

{

color:blue;

}

</style>

<title>Edit Repeater</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div class=”content”>

<asp:Repeater

id=”rptMovies”

DataSourceID=”srcMovies”

Runat=”server” OnItemCommand=”rptMovies_ItemCommand”

OnItemDataBound=”rptMovies_ItemDataBound” OnDataBinding=”rptMovies_DataBinding”>

<HeaderTemplate>

<table class=”movies”>

<tr>

<th>Title</th>

<th>Director</th>

Trang 5

<th>In Theaters</th>

</tr>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td>

<asp:TextBox

id=”txtTitle”

Text=’<%#Eval(“Title”)%>’

Runat=”server” />

</td>

<td>

<asp:TextBox

id=”txtDirector”

Text=’<%#Eval(“Director”)%>’

Runat=”server” />

</td>

<td>

<asp:CheckBox

id=”chkInTheaters”

Checked=’<%#Eval(“InTheaters”)%>’

Runat=”server” />

</td>

<td>

<asp:LinkButton

id=”lnkUpdate”

CommandName=”Update”

Text=”Update”

Runat=”server” />

&nbsp;|&nbsp;

<asp:LinkButton

id=”lnkDelete”

CommandName=”Delete”

Text=”Delete”

OnClientClick=”return confirm(‘Are you sure?’);”

Runat=”server” />

</td>

</tr>

</ItemTemplate>

<FooterTemplate>

<tr>

<td>

<asp:TextBox

id=”txtTitle”

Runat=”server” />

</td>

Trang 6

<td>

<asp:TextBox

id=”txtDirector”

Runat=”server” />

</td>

<td>

<asp:CheckBox

id=”chkInTheaters”

Runat=”server” />

</td>

<td>

<asp:LinkButton

id=”lnkInsert”

CommandName=”Insert”

Text=”Insert”

Runat=”server” />

</td>

</tr>

</table>

</FooterTemplate>

</asp:Repeater>

<asp:SqlDataSource

id=”srcMovies”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT Id,Title,Director,InTheaters

FROM Movies”

UpdateCommand=”UPDATE Movies SET Title=@Title,

Director=@Director,InTheaters=@InTheaters

WHERE Id=@Id”

InsertCommand=”INSERT Movies (Title,Director,InTheaters)

VALUES (@Title,@Director,

DeleteCommand=”DELETE Movies WHERE Id=@Id”

Runat=”server”>

<UpdateParameters>

<asp:Parameter Name=”Id” />

<asp:Parameter Name=”Title” />

<asp:Parameter Name=”Director” />

<asp:Parameter Name=”InTheaters” />

</UpdateParameters>

<InsertParameters>

<asp:Parameter Name=”Title” />

<asp:Parameter Name=”Director” />

<asp:Parameter Name=”InTheaters” />

</InsertParameters>

<DeleteParameters>

Trang 7

<asp:Parameter Name=”Id” />

</DeleteParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

In Listing 13.5, the ItemDataBound event handler builds a collection of primary keys from

the data source The collection of primary keys is stored in ViewState so that they will be

available after a postback to the server

The DataBinding event handler clears the primary key collection when the Repeater is

rebound to its data source (after a record is updated or deleted) If you don’t clear the

collection, you get duplicates of the primary keys and an exception is raised

The ItemCommand event handler takes care of processing the button click events When

you click an Insert, Update, or Delete button, the event bubbles up and raises the

ItemCommmand event The ItemCommand event handler grabs the values from the form fields

and calls the Insert(), Update(), or Delete() methods of the SqlDataSource control

Using the DataList Control

The DataList control, like the Repeater control, is template driven Unlike the Repeater

control, by default, the DataList renders an HTML table Because the DataList uses a

particular layout to render its content, you are provided with more formatting options

when using the DataList control

In this section, you learn how to use the DataList control to display data You also learn

how to render database records in both single-column and multicolumn HTML tables We

also explore how you can edit data with the DataList control

To display data with the DataList control, you must supply the control with an

ItemTemplate The contents of the ItemTemplate are rendered for each data item from the

data source

For example, the page in Listing 13.6 uses a DataList to display the contents of the

Movies database table The ItemTemplate displays the values of the Title, Director, and

BoxOfficeTotals columns (see Figure 13.5)

Trang 8

LISTING 13.6 ShowDataList.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Show DataList</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:DataList

id=”dlstMovies”

DataSourceID=”srcMovies”

Runat=”server”>

<ItemTemplate>

<h1><%#Eval(“Title”)%></h1>

Directed by:

<%#Eval(“Director”) %>

<br />

FIGURE 13.5 Displaying database records with the DataList control

Trang 9

Box Office Totals:

<%#Eval(“BoxOfficeTotals”,”{0:c}”) %>

</ItemTemplate>

</asp:DataList>

<asp:SqlDataSource

id=”srcMovies”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT Title,Director,BoxOfficeTotals

FROM Movies”

Runat=”server” />

</div>

</form>

</body>

</html>

The DataList in Listing 13.6 renders an HTML table Each data item is rendered into a

separate table cell (<td> tag) The rendered output of the DataList control in Listing 13.6

looks like this:

<table id=”dlstMovies” cellspacing=”0” border=”0”

style=”border-collapse:collapse;”>

<tr>

<td>

<h1>Titanic</h1>

Directed by:

James Cameron

<br />

Box Office Totals:

$600,000,000.00

</td>

</tr>

<tr>

<td>

<h1>Star Wars</h1>

Directed by:

George Lucas

<br />

Box Office Totals:

$500,000,000.00

</td>

</tr>

</table>

Trang 10

The default behavior of the DataList control is to render an HTML table However, you

can override this default behavior and display the contents of each data item in a separate

HTML <span> tag This approach is illustrated in Listing 13.7

LISTING 13.7 ShowFlowDataList.aspx

<%@ Page Language=”C#” %>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Show Flow DataList</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:DataList

id=”dlstMovies”

DataSourceID=”srcMovies”

RepeatLayout=”Flow”

Runat=”server”>

<ItemTemplate>

<%#Eval(“Title”)%>

</ItemTemplate>

</asp:DataList>

<asp:SqlDataSource

id=”srcMovies”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT Title FROM Movies”

Runat=”server” />

</div>

</form>

</body>

</html>

The DataList control in Listing 13.7 includes a RepeatLayout property that has the value

Flow Each movie title is rendered in a <span> tag followed by a line-break tag (<br>)

The RepeatLayout property accepts one of the following two values:

Table—Data Items are rendered in HTML table cells

Flow—Data Items are rendered in HTML <span> tags

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

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN