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

Professional ASP.NET 3.5 in C# and Visual Basic Part 37 pptx

10 300 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 402,54 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 7-21: Building connection strings using ConnectionStringBuilder VB ’ Retrieve an existing connection string into a Connection String Builder Dim builder As New System.Data.SqlCli

Trang 1

SqlCacheDependencyproperty to create SQL dependencies You can learn more about ASP.NET

caching features in Chapter 23

Storing Connection Information

In ASP.NET 1.0/1.1, Microsoft introduced theweb.configfile as a way of storing application configura-tion data in a readable and portable format Many people quickly decided that theweb.configfile was a great place to store things like the database connection information their applications use It was easy to access from within the application, created a single central location for the configuration data, and it was

a cinch to change just by editing the XML

Although all those advantages were great, several drawbacks existed First, none of the information

in theweb.configfile can be strongly typed It was, therefore, difficult to find data type problems within the application until a runtime error occurred It also meant that developers were unable to use the power

of IntelliSense to facilitate development A second problem was that although theweb.configfile was

secured from access by browsers (it cannot be served up by Internet Information Server), the data within the file was clearly visible to anyone who had file access to the Web server

Microsoft has addressed these shortcomings of theweb.configfile by adding features specifically

designed to make it easier to work with and secure connection string information in theweb.config

file Because database connection information is so frequently stored in theweb.configfile, starting with ASP.NET 2.0, a new configuration section was added in that file The<connectionStrings>section is

designed specifically for storing the connection string information

If you examine yourweb.configfile, you should see at least one connection string already in the

<connectionStrings>section because our example told the Data Connection Wizard to store

connec-tions in theweb.configfile Listing 7-19 shows how ASP.NET stores a connection string

Listing 7-19: A typical connection string saved in the web.config file

<connectionStrings>

<add name="AppConnectionString1" connectionString="Server=localhost;

User ID=sa;Password=password;Database=Northwind;

Persist Security Info=True" providerName="System.Data.SqlClient" />

</connectionStrings>

Using a separate configuration section has several advantages First, NET exposes theConnectionString section using theConnectionStringSettingsclass This class contains a collection of all the connection strings entered in yourweb.configfile and allows you to add, modify, or remove connection strings at runtime Listing 7-20 shows how you can access and modify connection strings at runtime

Listing 7-20: Modifying connection string properties at runtime

VB

<%@ Page Language="VB" %>

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Continued

Trang 2

If (Not Page.IsPostBack) Then

’ Create a new ConnectionStringSettings object and populate it Dim conn As New ConnectionStringSettings()

conn.ConnectionString = _

"Server=localhost;User ID=sa;Password=password" & _

"Database=Northwind;Persist Security Info=True"

conn.Name = "AppConnectionString1"

conn.ProviderName = "System.Data.SqlClient"

’ Add the new connection string to the web.config ConfigurationManager.ConnectionStrings.Add(conn) End If

End Sub

</script>

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

<head runat="server">

<title>Modifying the Connection String</title>

</head>

<body>

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

<div>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server">

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

C#

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

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack) {

// Create a new ConnectionStringSettings object and populate it ConnectionStringSettings conn = new ConnectionStringSettings();

conn.ConnectionString =

"Server=localhost;User ID=sa;Password=password; " +

"Database=Northwind;Persist Security Info=True";

conn.Name = "AppConnectionString1";

conn.ProviderName = "System.Data.SqlClient";

// Add the new connection string to the web.config ConfigurationManager.ConnectionStrings.Add(conn);

} }

</script>

Trang 3

As you can see, theConfigurationManagerclass now has aConnectionStringscollection property

in addition to theAppSettingscollection used in ASP.NET 1.0 This new collection contains all the

connection strings for your application

Additionally, ASP.NET makes it much easier to build connection strings using strongly typed properties

at runtime, and easier to add them to theweb.configfile Using the newSqlConnectionStringBuilder class, you can build connection strings and then add them to yourConnectionStringSettings

collec-tion Listing 7-21 shows how you can use theConnectionStringBuilderclass to dynamically assemble connection strings at runtime and save them to yourweb.configfile

Listing 7-21: Building connection strings using ConnectionStringBuilder

VB

’ Retrieve an existing connection string into a Connection String Builder

Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder()

’ Change the connection string properties

builder.DataSource = "localhost"

builder.InitialCatalog = "Northwind1"

builder.UserID = "sa"

builder.Password = "password"

builder.PersistSecurityInfo = true

’ Save the connection string back to the web.config

ConfigurationManager.ConnectionStrings("AppConnectionString1").ConnectionString = _

builder.ConnectionString

C#

// Retrieve an existing connection string into a Connection String Builder

System.Data.SqlClient.SqlConnectionStringBuilder builder = new

System.Data.SqlClient.SqlConnectionStringBuilder();

// Change the connection string properties

builder.DataSource = "localhost";

builder.InitialCatalog = "Northwind1";

builder.UserID = "sa";

builder.Password = "password";

builder.PersistSecurityInfo = true;

// Save the connection string back to the web.config

ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString =

builder.ConnectionString;

Using Bound List Controls

with Data Source Controls

The data source controls included in ASP.NET really shine when you combine them with the Bound List controls also included in ASP.NET This combination allows you to declaratively bind your data source

to a bound control without ever writing a single line of C# or VB code

Trang 4

Fear not, those of you who like to write code You can always use the familiarDataBind()method to

bind data to the list controls In fact, that method has even been enhanced to include a Boolean overload

that allows you to turn the data-binding events on or off This enables you improve the performance of

your application if you are not using any of the binding events.

GridView

With ASP.NET 1.0/1.1, Microsoft introduced a new set of server controls designed to make developers

more productive One of the most popular controls was the DataGrid With this one control, you could

display an entire collection of data, add sorting and paging, and perform inline editing Although this

new functionality was great, many tasks still required that the developer write a significant amount of

code to take advantage of this advanced functionality

Beginning with ASP.NET 2.0, Microsoft took the basic DataGrid and enhanced it, creating a new server

control called the GridView This control makes it even easier to use those advanced DataGrid features,

usually without having to write any code, and it also adds a number of new features

Displaying Data with the GridView

Start using the GridView by dragging the control onto the designer surface of an ASP.NET Web page

You are prompted to select a data source control to bind to the grid In this sample, you use the

SqlData-Source control created earlier in the chapter

After you assign the GridView a data source, notice a number of changes First, the GridView changes

its design-time display to reflect the data exposed by the data source control assigned to it Should

the schema of the data behind the data source control ever change, you can use the GridView’s Refresh

Schema option to force the grid to redraw itself based on the new data schema Second, the GridView’s

smart tag has additional options for formatting, paging, sorting, and selection

Switch the page to Source view in Visual Studio to examine GridView’s code Listing 7-22 shows the code

generated by Visual Studio

Listing 7-22: Using the GridView control in an ASP.NET Web page

<html>

<head runat="server">

<title>Using the GridView server control</title>

</head>

<body>

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

<div>

<asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1"

DataKeyNames="CustomerID" AutoGenerateColumns="False">

<Columns>

<asp:BoundField ReadOnly="True" HeaderText="CustomerID"

DataField="CustomerID"

SortExpression="CustomerID"></asp:BoundField>

<asp:BoundField HeaderText="CompanyName" DataField="CompanyName"

SortExpression="CompanyName"></asp:BoundField>

Trang 5

<asp:BoundField HeaderText="ContactName" DataField="ContactName"

SortExpression="ContactName"></asp:BoundField>

<asp:BoundField HeaderText="ContactTitle" DataField="ContactTitle"

SortExpression="ContactTitle"></asp:BoundField>

<asp:BoundField HeaderText="Address" DataField="Address"

SortExpression="Address"></asp:BoundField>

<asp:BoundField HeaderText="City" DataField="City"

SortExpression="City"></asp:BoundField>

<asp:BoundField HeaderText="Region" DataField="Region"

SortExpression="Region"></asp:BoundField>

<asp:BoundField HeaderText="PostalCode" DataField="PostalCode"

SortExpression="PostalCode"></asp:BoundField>

<asp:BoundField HeaderText="Country" DataField="Country"

SortExpression="Country"></asp:BoundField>

<asp:BoundField HeaderText="Phone" DataField="Phone"

SortExpression="Phone"></asp:BoundField>

<asp:BoundField HeaderText="Fax" DataField="Fax"

SortExpression="Fax"></asp:BoundField>

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server"

SelectCommand="SELECT * FROM [Customers]"

ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>"

DataSourceMode="DataSet"

ConflictDetection="CompareAllValues" EnableCaching="True"

CacheKeyDependency="MyKey" CacheDuration="Infinite">

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

Figure 7-13 shows what your Web page looks like when you execute the code in the browser

The GridView includes several events that are raised when the data binding occurs These are described

in the following table

Event Name Description

DataBinding Raised as the GridViews data-binding expressions are about to be evaluated

RowCreated Raised each time a new row is created in the grid Before the grid can be

rendered, aGridViewRowobject must be created for each row in the control

TheRowCreatedevent allows you to insert custom content into the row as it is being created

RowDataBound Raised as eachGridViewRowis bound to the corresponding data in the data

source This event allows you to evaluate the data being bound to the current row and to affect the output if you need to

DataBound Raised after the binding is completed and the GridView is ready to be

rendered

Trang 6

Figure 7-13

TheRowDataBoundevent is especially useful, enabling you to inject logic into the binding process for

each row being bound Listing 7-23 shows how you can use theRowDataBoundto examine the data being

bound to the current row and to insert your own logic The code checks to see if the databases Region

column isnull; if it is, the code changes theForeColorandBackColorproperties of the GridView rows

Listing 7-23: Using the RowDataBound to examine the data being bound to the current

row and to insert your own logic

VB

<script runat="server">

Protected Sub GridView1_RowDataBound(ByVal sender As Object, _

ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

’Test for null since gridview rows like the

’ Header and Footer will have a null DataItem

If (e.Row.DataItem IsNot Nothing) Then

’Used to verify the DataItem object type System.Diagnostics.Debug.WriteLine(e.Row.DataItem.ToString())

Trang 7

’When bound to a SqlDataSource, the DataItem

’ is generally returned as a DataRowView object Dim drv As System.Data.DataRowView = _

CType(e.Row.DataItem, System.Data.DataRowView)

If (drv("Region") Is System.DBNull.Value) Then e.Row.BackColor = System.Drawing.Color.Red e.Row.ForeColor = System.Drawing.Color.White End If

End If End Sub

</script>

C#

<script runat="server">

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

//Test for null since gridview rows like the // Header and Footer will have a null DataItem

if (e.Row.DataItem != null) {

//Used to verify the DataItem object type System.Diagnostics.Debug.WriteLine(e.Row.DataItem.ToString());

//When bound to a SqlDataSource, the DataItem // is generally returned as a DataRowView object System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem;

if (drv["Region"] == System.DBNull.Value) {

e.Row.BackColor = System.Drawing.Color.Red;

e.Row.ForeColor = System.Drawing.Color.White;

} } }

</script>

The GridView also includes events that correspond to selecting, inserting, updating, and deleting data

You learn more about these events later in the chapter

Using the EmptyDataText and EmptyDataTemplate Properties

In some cases, the data source bound to the GridView may not contain any data for the control to bind

to Even in these cases, you may want to provide the end user with some feedback, informing him that

no data is present for the control to bind to The GridView offers you two techniques to do this

You first option is to use theEmptyDataTextproperty The property allows you to specify a string of text that is displayed to the user when no data is present for the GridView to bind to When the ASP.NET

page loads and the GridView determines there was no data available in its bound data source, it creates

a special DataRow containing theEmptyDataTextvalue and displays that to the user Listing 7-24 shows how you can add this property to the GridView

Trang 8

Listing 7-24: Adding EmptyDataText to the GridView

<asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1"

DataKeyNames="CustomerID" AutoGenerateColumns="False"

EmptyDataText="No data was found using your query">

The other option is to use an EmptyDataTemplate template to completely customize the special row the

user sees when no data exists for the control to bind to

A control template is simply a container that gives you the capability to add other content such as

text, HTML controls, or even ASP.NET controls The GridView control provides you with a variety

of templates for various situations, including the EmptyDataTemplate template You examine these

templates throughout the rest of the GridView control section of this chapter.

You can access the template from the Visual Studio 2008 design surface in two ways The first option is

to right-click the GridView control, expand the Edit Template option in the context menu, and select the

EmptyDataTemplate item from the menu This procedure is shown in Figure 7-14

Figure 7-14

The other option for opening the template is to select the Edit Templates option from the GridView smart

tag Selecting this option puts the GridView into template editing mode and presents you with a

dialog from which you can choose the specific template you wish to edit Simply select

EmptyData-Template from the drop-down list, as shown in Figure 7-15

After you have entered template editing mode, you can simply add your custom text and/or controls

to the template editor on the design surface When you have finished editing the template, simply

right-click, or open the GridViews smart tag and select the End Template Editing option

Trang 9

Figure 7-15

Switching to Source view, you see that an<EmptyDataTemplate>element has been added to the Grid-View control The element should contain all the controls and HTML that you added while editing the

template Listing 7-25 shows an example of anEmptyDataTemplate

Listing 7-25: Using EmptyDataTemplate

<EmptyDataTemplate>

<table style="width: 225px">

<tr>

<td colspan="2">

No data could be found based on your query parameters

Please enter a new query.</td>

</tr>

<tr>

<td style="width: 162px">

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>

<td style="width: 102px">

<asp:Button ID="Button1" runat="server" Text="Search" /></td>

</tr>

</table>

</EmptyDataTemplate>

You could, of course, have also added the template and its contents while in Source view

Enabling GridView Column Sorting

The capability to sort data is one of the most basic tools users have to navigate through a significant

amount of data The DataGrid control made sorting columns in a grid a relatively easy task, but the

GridView control takes it one step further Unlike using the DataGrid, where you are responsible for

coding the sort routine, to enable column sorting in this grid, you just set theAllowSortingattribute to True The control takes care of all the sorting logic for you internally Listing 7-26 shows how to add this attribute to your grid

Listing 7-26: Adding sorting to the GridView control

<asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1"

DataKeyNames="CustomerID" AutoGenerateColumns="False"

AllowSorting="True">

Trang 10

After enabling sorting, you see that all grid columns have now become hyperlinks Clicking a column

header sorts that specific column Figure 7-16 shows your grid after the data has been sorted by country

Figure 7-16

GridView sorting has also been enhanced in a number of other ways The grid can handle both ascending

and descending sorting If you repeatedly click on a column header, you cause the sort order to switch

back and forth between ascending and descending The GridView’sSortmethod can also accept multiple

SortExpressionsto enable multicolumn sorting Listing 7-27 shows how you can use the GridView’s

sorting event to implement a multicolumn sort

Listing 7-27: Adding multicolumn sorting to the GridView

VB

<script runat="server">

Protected Sub GridView1_Sorting(ByVal sender As Object, _

ByVal e As GridViewSortEventArgs)

Dim oldExpression As String = GridView1.SortExpression Dim newExpression As String = e.SortExpression

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

TỪ KHÓA LIÊN QUAN