Tuesday, February 9, 2010

Converting Single-File Web Forms Pages to Code-Behind Pages

Sometimes I face some problem with single web form(only aspx page no code behind page) page. I am used to work in code-behind page,so that I try to solve this problem in may way but at last I found the solution in msdn. So I want to share this portion of the article with you:

It might be that the only thing you want to do with a single-file Web Forms page in Visual Studio is to convert it to a code-behind Web Forms page. Using Visual Studio to convert a single-file page to a code-behind page automatically accomplishes the following:

1. Generates a code-behind class file for the page.
2. Adds the appropriate page directives to the page so it is linked with the code-behind file.

The conversion does not do the following:

a. Convert inline code to code in the class file.

To convert a single-file .aspx page to code-behind

1. Open an existing project, or create a new ASP.NET Web application.
2. On the Project menu, click Add Existing Item.
3. Browse to the .aspx page that you want to convert.
4. Click Open. Visual Studio will display the following message:

There is no class file in the project associated with the Web Form ''. Create a new class file now?
5. Click Yes. Visual Studio will create the code-behind file and link it to the .aspx page.
6. Click the Show All Files button in Solution Explorer, and expand the tree below your newly converted file to see the code-behind class file.
7. Open the new .aspx file.
8. In HTML view, inspect the file to make sure it contains an @ Page directive at the top that contains the CodeBehind attribute.
9. Inspect the file to make sure it contains an HTML

element within the HTML tags. If not, add <form> and </form> tags around your controls, and in the opening tag, set the runat = "server" attribute so it looks similar to the following:

<form id="Form1" method="post" runat="server">

10. If the file contains calls to COM components — for example, ADO code — add the AspCompat attribute to the Page directive in HTML view:

<%@ Page [...] AutoEventWireup="false" AspCompat="true" %>

If you do not have any calls to COM components, you do not need to add the AspCompat attribute.
11. Move your code out of the .aspx file and into the .aspx.vb or .aspx.cs file.
If you are working with a legacy file that contains ASP code, you must convert it to ASP.NET code before you can move it to the code-behind page. Converting to ASP.NET also will help you to take advantage of dynamic browser customization, state maintenance, and other features the ASP.NET server controls provide. For details, see Converting ASP to ASP.NET.
12. Save the form, compile the project, and view it in the browser.
13. Check for issues that appear in the Task List window, and resolve them.

For more info please visit:
http://msdn.microsoft.com/en-us/library/aa290385%28VS.71%29.aspx

2 comments:

Dan said...
This comment has been removed by the author.
Dan said...

I don't think this works for 2010.

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...