Dd An Event Handler O An Aspnet Page

You can add code to your ASP.NET pages to respond to your user's interaction, for example, clicking a button or an image. In Web development, you are presented with a challenge when programming for events. If you want to process events on the Web server, you need to forward the event captured by the browser to the Web server along with the state of the page.

Processing user interactions to elements on your Web page (events) on the Web server is simplified for you in ASP.NET. To configure this, you need to do a few things to properly configure your Web Form. First, make sure your HTML form has the runat attribute set to Server. Next you need to add an additional attribute to the element that is capturing the user interaction. This attribute determines what function is called when the event occurs. For example, you can add an OnClick attribute to the button that calls a function to process the form. From within the function, you can set the <span> placeholder's InnerHTML property to display some HTML based on results of processing the event.

Note that this is only one of several ways to configure events for your Web Forms.

ADD AN EVENT HANDLER TO AN ASP.NET PAGE

% Untitled - Notepad

% Untitled - Notepad

File | Edit Format Help New Ctrl+N

Open...

Ctrl+O

Save

Ctrl+S

5ave As,..

File | Edit Format Help New Ctrl+N

"3

Page

j?Jxj

Look in: ¡r^J Code Templates

¿1 BusinessTitlesT emplate.aspx" lf| CommentsT emplate. aspx*|—

_ ConvertToWebPageT emplate

3 CreateApplicationTemplate.as| x Jè) CreateCookieCollectionT empli 3 CreateCookieT emplate. aspx 3 CreateS essionT emplate. aspx

DatagridT emplate. aspx ffi DeclareServerSideObiectGlob ©DefaultTemplate.htm 1] D eleteApplicationT emplate. as| »

¿1 BusinessTitlesT emplate.aspx" lf| CommentsT emplate. aspx*|—

_ ConvertToWebPageT emplate

3 CreateApplicationTemplate.as| x Jè) CreateCookieCollectionT empli 3 CreateCookieT emplate. aspx 3 CreateS essionT emplate. aspx

DatagridT emplate. aspx ffi DeclareServerSideObiectGlob ©DefaultTemplate.htm 1] D eleteApplicationT emplate. as| »

|AII Files

□ Start your text editor.

-0 Click File O Open.

D eleteS essionT emplate. aspx G enericT emplate. asp G enericT emplate. aspx G enericT emplate. htm G etRequestParamsT emplate. aspx R eadApplicatioriT emplate. aspx

ReadCook ReadCook eCollectionT emplate.aspx ieT emplate. aspx R eadS essionT emplate. aspx S uggestionsT emplate. htm S uggestionsT emplate 1 .aspx

Cancel ^

CommentsTemplate.aspx - Notepad

File Edit Format Help

Q Click 0 to select All Files from the drop-down list.

Click to select a template.

^SCRIPT LANGUAGE-'C#' RUNAT-'Setver"> I

void SubmitBtn_Click(Object sender, EventArgs e){

spanComment.lnnerHtml - "Here are the details we received.<BR/><BR/>" + "First Name: " + textboxFirstName.Text + "<BR/>" + "Last Name " + textboxLastName.Text + "iBR/-" + "Comments. " + textareaComments.Value + "<BF:/>", }_

<FONT FACE ="Verdana">

iH3>Welcome to vww/.mylifetimegoals.com</H3>

What comments do you have about our site*?

<FORM ACTION-'SubmitReceiveComments.aspx" METHOD="POST" RUNAT-'Setver">

First Name: -=ASP:TEXTBOX ID-'textboxFirstName" RUNAT-'Server" /="=BR/=> Last Name. <ASP:TEXTBOX ID="te)!tboxLastName" RUNAT="Server" !><Pt> Comments:-:?/>

cTEXTAREA COLS-'50" ROWS-'5" ID-'textareaComments" RUNAT-'Server":=

<ASP:BUTTON TEXT="Send Comments"|ONCLICK=" SubmltBtn_Click"| RUNAT-'Setver" l>

~Q Declare the language used for the page.

_Q Declare a script block to run server-side.

Create an event handler.

'-E Add the ONCLICK event handler to the form and call the event created.

Was this article helpful?

0 0

Post a comment