Monday 30 July 2012

How to add a web form to a web site

If you start a web site from the ASP.NET Empty Web Site template, the first thing you’ll want to do is to add a web form to the web site. To do that, you use the Add New Item dialog box shown in figure 2-2. From this dialog box, you select the Web Form template. Then, you enter the name you want to use for the new form and click the Add button to add it to your web site. When you add a new web form, be sure that the language setting is Visual C# and that the Place Code in Separate File box is checked.


These settings are easy to overlook, but difficult to change manually if they’re set wrong when you create the page. If you select the Place Code in Separate File box, two files are added to your project. When I added the form shown in this figure, for example, files named Default.aspx and Default.aspx.cs were added. The Default.aspx file contains the HTML and asp code that defines the form, and the Default.aspx.cs file contains the C# code that determines how the form works.

Then, Visual Studio displays the aspx file for the web form as you’ll see in the next figure. To add an existing web form from another web site, you can use the second procedure in this figure. You might want to do that if you need to create a form that’s similar to a form in another web site. When you add the aspx file for a form, the code-behind file is added too. Then, you can modify the aspx and C# code so the form works the way you want it to in your new web site.

No comments:

Categories