Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

ADD SERVER CONTROLS TO A WEB FORM

he power of Web Forms comes into play when you Tstart leveraging the built-in capabilities of server

controls. Server controls have rich capabilities that are typically available only in Win32-based applications or what would be available in ActiveX controls.

For rich user interfaces, you can either write very complicated DHTML or use ActiveX controls. Natively, only Internet Explorer is an ActiveX container; therefore, it is not widely accepted in Web development, leaving a wide gap in capabilities between the user interface richness in Win32 applications versus Web applications. To address this gap, ASP.NET applications provide Web server controls. Server controls send standard HTML to the client versus an

embeded object that requires special browser or operating system runtime capabilities to host the object. You can configure server controls through their attributes or serverside code.

After you add a server control to a Web Form, you have several ways to configure the control. With the simplest standard input controls — for example, the TextBox, Button, and CheckBox — you use the Properties window typically docked in the lower-right hand side of your VS integrated development environment (IDE). For more sophisticated server-side controls, you can configure advanced options in the Property Builder or Auto Format dialog boxes.

ADD SERVER CONTROLS TO A WEB FORM

Forms tab

ˇ Right-click the Button

controls.

control and select Properties.

Button in

 

on the

PROGRAMMING WEB APPLICATIONS 11

The following example demonstrates the use of the Panel Web server control, which is useful for pages that view different content based on the state of the page. To get the full code sample, see the companion CD-ROM.

TYPE THIS:

<SCRIPT LANGUAGE="C#" RUNAT="Server">

void cmdDescription_Click(object Source, EventArgs e)

{

if (pnlDescription.Visible == true)

{

pnlDescription.Visible = false; cmdDescription.Text = "Show Photo Description";

}

else

{

pnlDescription.Visible = true; cmdDescription.Text = "Hide Photo Description";

}

}

</SCRIPT>

RESULT:

The resulting panel that is show is rendered in the following <div> tag:

<div id="pnlDescription" style="backgroundcolor:SkyBlue;height: 50px;width:300px;">

Here is where the description displays:

</div>

Properties

Text

 

 

 

 

The Properties window

Build and browse the

appears.

Web page.

 

Á Change the Text value

Note: See page 220 for more

 

for the button to Click Me.

information on building and

 

 

browsing a Web page.

The Web page appears with the Button server control in the Preview window.

223

C#

RESPOND TO AN EVENT IN SERVER-SIDE CONTROLS

ou can implement event handlers to respond to user Yinteraction with your Web Form. Some common

events available to program are mouse clicks on buttons, or the mouse moving over text. Using event handlers, a common object-oriented programming practice, creates a more efficient programming model. This model only executes code when the corresponding event fires for the handler. Without this model, you must use procedural style coding, which evaluates code from top to bottom and requires you to run code to determine if you should call a procedure.

You can implement event handlers in the code-behind pages. To create an event handler in the code-behind page, you need to assign a programmatic id to the server-side control. You do this giving a value for the id attribute on the HTML tag for the server-side control.

ASP.NET uses the id for the control with the event name to construct the event handler. For example, a server control with id = "cmdTest" needs an event handler called cmdTest_Click() to respond to a user clicking a Button server control. Inside this handler or procedure,

you implement code that needs to run in response to the event firing.

RESPOND TO AN EVENT IN SERVER-SIDE CONTROLS

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Add a Button control to

 

 

 

Double-click the Button

 

the Web page.

server control.

PROGRAMMING WEB APPLICATIONS 11

You can create a code-behind page that responds to an event using the following bare bones of implementation. This is hand-crafted code and not the automatically generated code that comes from the VS .NET environment. You first create the .aspx page RespondToEvent_ai.aspx with the first block of code. Next, you create the supporting code-behind page, RespondToEvent_ai.aspx.cs.

You then place both of these files into an existing ASP.NET site to receive the results into the Web page.

TYPE THIS:

<%@ Page Inherits="RespondToEvent_ai" Src="RespondToEvent_ai.aspx.cs" %> <html>

<head>

</head>

<body>

<form runat="Server"> <P/>

<asp:labelID="lblGreeting" runat="Server" /> </form>

</body>

</html>

TYPE THIS:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

public class RespondToEvent_ai : Page { public Label lblGreeting;

public void Page_Load(object Source, EventArgs e) { lblGreeting.Text="Welcome to MySharePhotoAlbum.com";

}

}

RESULT:

A page that displays the following:

Welcome to MySharePhotoAlbum. com

The Button1_Click function is created.

ˇ Type Label1.Text = "Click event fired" in the

Button1_Click function to update the label when the button is clicked.

Á Build and browse the Web page.

Note: See page 220 for more information on building and browsing a Web page.

The Web page appears with the TextBox and Button controls in the Preview window.

Click the button.

The text box is updated to indicate that the Click event was fired.

225

Соседние файлы в папке c#