|
ASP.NET Futures (July 2007): Using Dynamic Languages with ASP.NET
This walkthrough provides you with an introduction to dynamic languages for ASP.NET.
It guides you through creating a simple page in Microsoft Visual Studio, adding
controls, and adding event handlers using dynamic languages.
Tasks illustrated in this walkthrough include:
- Adding controls to the default page.
- Adding event handlers in a separate code file, using dynamic languages.
- Adding a second page with event-handling code in the page.
Prerequisites
In order to complete this walkthrough, you will need:
- Microsoft Visual Studio 2005 or Visual Web Developer Express Edition.
- The ASP.NET Futures release (May 2007 or later) installed on your computer. The .msi installer
file includes a Visual Studio Content Installer (.vsi) for creating a blank ASP.NET
Futures Web application in Visual Studio.
This walkthrough assumes that you have a general understanding of working in Visual
Web Developer. For an introduction, see
Walkthrough: Creating a Basic Page in Visual Web Developer.
Creating a Web Site
In this part of the walkthrough, you will create a Web site with a dynamic language
as the default language.
To create a Web site with a default ASP.NET Web page
- In Visual Studio (or Visual Web Developer), in the File
menu, click New Web Site. The New Web Site
dialog box is displayed.
- In the Language list, select a dynamic language. For example,
select IronPython to make IronPython the default language
for the Web site.
Note: You can use statically compiled languages in the same Web application
by creating pages and components in different programming languages.
- Under Visual Studio installed templates, click ASP.NET Web Site.
- In the Location box, select the File System
box, and then enter the name of the folder where you want to keep the pages of your
Web site. For example, type the folder name C:\BasicWebSite.
- Click OK. Visual Studio creates the folder and a new page
named Default.aspx.
Adding Controls to the Default Page
In this part of the walkthrough, you will add server controls to the page.
To add controls to the page
- Switch to Design view.
- In the Toolbox, from the Standard group, drag three controls
onto the page: a TextBox control, a
Button control, and a Label control.
- Put the insertion point above the TextBox control,
and then type Enter your name: to create a caption
for the text box.
Programming the Button Control
For this walkthrough, you will write code that reads the name that the user enters
into the text box and then displays the name in the Label
control.
To add a button event handler
- Right-click the page and click View Code to show the separate
code file. For example, if you are using IronPython, the file is Default.aspx.py.
The file contains a stub event handler for the Load
event of the page.
Note: In IronPython, pass is a placeholder that does nothing.
- Replace the stub event handler with the following code to set the label text when
the page is initialized:
def Page_Load(sender, e):
if not IsPostBack:
Label1.Text = "...Your name here..."
IronPython
- Add the following code to create an event handler for the button's
Click
event:
def Button1_Click(sender, e):
Label1.Text = Textbox1.Text
IronPython
In this release, event handlers must be coded and bound manually. You cannot create
them by double-clicking a control in Design view or by selecting an event in the
Properties window.
Because dynamic languages do not have typed parameters and variables, you do not
need to know the type of the event argument object.
Note: In this release, IntelliSense support for dynamically typed variables
is limited. You can press CTRL+SPACE to get a list of code elements that are currently
in scope.
- Switch to Default.aspx and go to Source view, and then bind the event handler by
adding an
OnClick attribute to the Button control markup, as
shown in the following example:
<form id="form1" runat="server">
<div>
Enter your name:<br />
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click"/><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label">
</asp:Label>
<br />
</div>
</form>
- Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
- Enter a name into the text box and click the button. The name you entered is displayed
in the Label control. If the name does not
appear, check the spelling of the event handler in the
OnClick attribute.
- In the browser, optionally view the source of the page you are running.
- Close the browser.
Form Example in IronPython
Form Example in Managed JScript
Adding a Page Without a Separate Code File
In this part of the walkthrough, you will add a page that contains its own code.
To add a Web page with its own code
- In Solution Explorer, right-click the name of the Web site, and then click Add New Item.
- Under Visual Studio installed templates, click Web Form.
- In the Name box, type SecondWebPage.
- In the Language list, select a dynamic language such as
IronPython.
- Clear the Place code in separate file check box to create
a single-file page with the code and markup in the same page.
- Click Add. Visual Studio creates the new page and opens it in Source view.
- Switch to Design view.
- Add a TextBox control, a
Button control, and a Label control,
as you did for the default page.
Programming the Button Control
For this walkthrough, you will add dynamic language code in a script block.
To add a default button event handler
- Switch to Source view.
- Add the following code to initialize the label and to create an event handler for
the button's Click event.
<script runat="server">
def Page_Load(sender, e):
if not IsPostBack:
Label1.Text = "...Your name here..."
def Button1_Click(sender, e):
Label1.Text = Textbox1.Text
</script>
IronPython
- In the Button control, bind the event handler
by adding the
OnClick attribute, as you did
previously in this walkthrough. The following example shows the markup.
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click"/><br />
- Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
See Also
Introduction to Dynamic Languages for ASP.NET
|