Connecting Tech Pros Worldwide Forums | Help | Site Map

Converting from ASP to ASP.net

Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84
#1: Jun 26 '09
Hi,
I am trying to convert my classic asp code to .net. I have created user controls and put following code in one of the user controls.

Expand|Select|Wrap|Line Numbers
  1. <div id="buttons">
  2. <ul id="topImages" class="menu" style="margin-left: -2px;">
  3. <li>
  4. <img id="home" alt="Home" class="top" onclick="javascript:location.href='default.asp'" onmouseout="changeImage(this.id,'home_top<%=home%>.png');" onmouseover="changeImage(this.id,'home_top_over.png');" src="http://bytes.com/images/home_top<%=home%>.png" ></li>
  5. <li><img id="web" alt="Services" class="top" onclick="javascript:location.href='services.asp'" onmouseout="changeImage(this.id,'services_top<%=services%>.png');" onmouseover="changeImage(this.id,'services_top_over.png');" src="http://bytes.com/images/services_top<%=services%>.png" >
  6. </li>
  7. </ul></div>
and if my home page I have some code like this.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ Register TagPrefix="uc" TagName="topMenu" Src="~/inc/topMenu.ascx" %>
  3.  
  4. <%
  5.     Dim home, services As String
  6.  
  7.     home = "_selected"
  8.     services = ""
  9. %>
  10.  
All my user controls are in a sub folder called "inc". In the topMenu user control it is giving me errors that it cannot recognize home and services variable.

What I need to do to make it work.

Thanks

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#2: Jun 26 '09

re: Converting from ASP to ASP.net


Could you please post the first line in your ASP page so that I can see how your project's configured.

If your project's configured to have 2 files created for your page (the ASP code and the VB.NET code in separate files) then you'll have to move the code in the <% %> to the VB.NET Code file. In this case your fist line in the page will have a CodeBehind attribute pointing to the VB file that should contain the page's VB.NET code. For example:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="ScratchPad.WebForm1" %>
If your project's not configured this way, then I believe you need to include script tags (<script runat="server"><script>) instead of the <% %>.

For example:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" %>
  2. <script runat="server">
  3.     Sub Button1_Click(ByVal sender As Object, _
  4.         ByVal e As System.EventArgs)
  5.         Label1.Text = "Server click handler called."
  6.     End Sub
  7. </script>
  8.  
  9. <body>
  10.   <form id="form1" runat="server">
  11.     <asp:Button ID="Button1" Runat="server" 
  12.       OnClick="Button1_Click" 
  13.         OnClientClick="return confirm('Ready to submit.')" 
  14.         Text="Test Client Click" />
  15.     <br />
  16.     <asp:Label ID="Label1" Runat="server" text="" />
  17.   </form>
  18. </body>
  19. </html>
Just post your first line and we'll be able to help you better.

-Frinny
Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84
#3: Jun 26 '09

re: Converting from ASP to ASP.net


This is the default.aspx code

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  2. <%@ Register TagPrefix="uc" TagName="homeText" Src="~/inc/homeText.ascx" %>
  3. <%@ Register TagPrefix="uc" TagName="pageHeader" Src="~/inc/header.ascx" %>
  4. <%@ Register TagPrefix="uc" TagName="leftMenu" Src="~/inc/leftMenu.ascx" %>
  5. <%@ Register TagPrefix="uc" TagName="footer" Src="~/inc/footer.ascx" %>
  6. <%@ Register TagPrefix="uc" TagName="headTags" Src="~/inc/headTags.ascx" %>
  7. <%@ Register TagPrefix="uc" TagName="topMenu" Src="~/inc/topMenu.ascx" %>
  8.  
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10.  
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head runat="server">
  13. <title>Omair Tech &amp; Designs</title>
  14. <uc:headTags ID="headTags" runat="server" />
  15. </head>
  16.  
  17. <%
  18.     Dim home, solutions, services, support, about, contact As String
  19.  
  20.     home = "_selected"
  21.     solutions = ""
  22.     services = ""
  23.     support = ""
  24.     about = ""
  25.     contact = ""
  26. %>
  27.  
  28. <body>
  29. <form id="userControlForm" runat="server">
  30. <table id="mainTable" align="center" border="0">
  31.     <tbody>
  32.     <tr>
  33.         <td>
  34.         <uc:pageHeader ID="pageHeader" runat="server" />
  35.         <uc:topMenu id="topMenu" runat="server" />
  36.  
  37.         <table id="mainContainer" border="0" cellspacing="0px">
  38.             <tbody>
  39.             <tr>
  40.                 <!-- Left Menu -->
  41.                 <uc:leftMenu ID="leftMenu" runat="server" />
  42.  
  43.                 <td id="mainContainerTextArea" valign="top">
  44.                     <uc:homeText id="homeText" runat="server" />
  45.  
  46.                 </td>
  47.             </tr>
  48.         </tbody>
  49.         </table>
  50.         </td>
  51.     </tr>
  52.     <uc:footer ID="footer" runat="server" />
  53. </tbody>
  54. </table>
  55. </form>
  56. </body>
  57. </html>
And this is the topMenu.ascx code. Every user control is in "inc" folder.

Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="VB" AutoEventWireup="false" CodeFile="topMenu.ascx.vb" Inherits="inc_topMenu" %>
  2.  
  3. <div id="buttons">
  4.     <ul id="topImages" class="menu" style="margin-left: -2px;">
  5.         <li>
  6.             <img id="home" alt="Home" class="top" onclick="javascript:location.href='default.aspx'" onmouseout="changeImage(this.id,'home_top<%=home%>.png');" onmouseover="changeImage(this.id,'home_top_over.png');" src="images/home_top<%=home%>.png" >
  7.         </li>
  8.         <li>
  9.             <img id="web" alt="Services" class="top" onclick="javascript:location.href='services.aspx'" onmouseout="changeImage(this.id,'services_top<%=services%>.png');" onmouseover="changeImage(this.id,'services_top_over.png');" src="images/services_top<%=services%>.png" >
  10.         </li>
  11.         <li>
  12.             <img id="app" alt="Solutions" class="top" onclick="javascript:location.href='solutions.aspx'" onmouseout="changeImage(this.id,'solutions_top<%=solutions%>.png');" onmouseover="changeImage(this.id,'solutions_top_over.png');" src="images/solutions_top<%=solutions%>.png" >
  13.         </li>
  14.         <li>
  15.             <img id="network" alt="Support" class="top" onclick="javascript:location.href='support.aspx'" onmouseout="changeImage(this.id,'support_top<%=support%>.png');" onmouseover="changeImage(this.id,'support_top_over.png');" src="images/support_top<%=support%>.png" >
  16.         </li>
  17.         <li>
  18.             <img id="about" alt="About us" class="top" onclick="javascript:location.href='aboutus.aspx'" onmouseout="changeImage(this.id,'about_top<%=about%>.png');" onmouseover="changeImage(this.id,'about_top_over.png');" src="images/about_top<%=about%>.png" >
  19.         </li>
  20.         <li>
  21.             <img id="consult" alt="Contact Us" class="top" onclick="javascript:location.href='contact.aspx'" onmouseout="changeImage(this.id,'contact_top<%=contact%>.png');" onmouseover="changeImage(this.id,'contact_top_over.png');" src="images/contact_top<%=contact%>.png" >
  22.         </li>
  23.     </ul>
  24. </div>
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#4: Jun 26 '09

re: Converting from ASP to ASP.net


When you're viewing the ASPX page (in design view not the ASPX code)...double click on the page.

This will open the Default.aspx.vb.

Your code belongs in the Default.aspx.vb file, not in the Default.aspx file.

You need to move the following into the .vb file:
Expand|Select|Wrap|Line Numbers
  1.     Dim home, solutions, services, support, about, contact As String
  2.  
  3.     home = "_selected"
  4.     solutions = ""
  5.     services = ""
  6.     support = ""
  7.     about = ""
  8.     contact = ""
I would recommend using proper scope for your variables. So, in your Default.aspx.vb file you should have something like:
Expand|Select|Wrap|Line Numbers
  1. Partial Public Class _Default
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Private solutions, services, support, about, contact As String
  5.     Private home As String = "_selected"
  6.  
  7.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8.  
  9.     End Sub
  10. End Class
  11.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#5: Jun 26 '09

re: Converting from ASP to ASP.net


Oh wait a sec!

The variables that you've defined in your Default page will not be accessible in your UserControl

You either need to move these variables to the User Control's .vb file....or you need to place them into the web.config file so that they can be accessed throughout every UserControl or Page in the project.
Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84
#6: Jun 26 '09

re: Converting from ASP to ASP.net


Actually I was able to fix it using following way

Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="VB" AutoEventWireup="false" CodeFile="topMenu.ascx.vb" Inherits="inc_topMenu" %>
  2.  
  3. <%
  4.  
  5.     Dim home, solutions, services, support, about, contact, selectedPage As String
  6.  
  7.     selectedPage = Request.Url.ToString
  8.     selectedPage = selectedPage.Substring(selectedPage.LastIndexOf("/") + 1)
  9.  
  10.     Select Case selectedPage
  11.         Case "default.aspx"
  12.             home = "_selected"
  13.         Case "solutions.aspx"
  14.             solutions = "_selected"
  15.         Case "services.aspx"
  16.             services = "_selected"
  17.         Case "support.aspx"
  18.             support = "_selected"
  19.         Case "about.aspx"
  20.             about = "_selected"
  21.         Case "contact.aspx"
  22.             contact = "_selected"
  23.     End Select
  24. %>
  25.  
  26. <div id="buttons">
  27.     <ul id="topImages" class="menu" style="margin-left: -2px;">
  28.         <li>
  29.             <img id="home" alt="Home" class="top" onclick="javascript:location.href='default.aspx'" onmouseout="changeImage(this.id,'home_top<%=home%>.png');" onmouseover="changeImage(this.id,'home_top_over.png');" src="images/home_top<%=home%>.png" >
  30.         </li>
  31.         <li>
  32.             <img id="web" alt="Services" class="top" onclick="javascript:location.href='services.aspx'" onmouseout="changeImage(this.id,'services_top<%=services%>.png');" onmouseover="changeImage(this.id,'services_top_over.png');" src="images/services_top<%=services%>.png" >
  33.         </li>
  34.         <li>
  35.             <img id="app" alt="Solutions" class="top" onclick="javascript:location.href='solutions.aspx'" onmouseout="changeImage(this.id,'solutions_top<%=solutions%>.png');" onmouseover="changeImage(this.id,'solutions_top_over.png');" src="images/solutions_top<%=solutions%>.png" >
  36.         </li>
  37.         <li>
  38.             <img id="network" alt="Support" class="top" onclick="javascript:location.href='support.aspx'" onmouseout="changeImage(this.id,'support_top<%=support%>.png');" onmouseover="changeImage(this.id,'support_top_over.png');" src="images/support_top<%=support%>.png" >
  39.         </li>
  40.         <li>
  41.             <img id="about" alt="About us" class="top" onclick="javascript:location.href='aboutus.aspx'" onmouseout="changeImage(this.id,'about_top<%=about%>.png');" onmouseover="changeImage(this.id,'about_top_over.png');" src="images/about_top<%=about%>.png" >
  42.         </li>
  43.         <li>
  44.             <img id="consult" alt="Contact Us" class="top" onclick="javascript:location.href='contact.aspx'" onmouseout="changeImage(this.id,'contact_top<%=contact%>.png');" onmouseover="changeImage(this.id,'contact_top_over.png');" src="images/contact_top<%=contact%>.png" >
  45.         </li>
  46.     </ul>
  47. </div>
  48.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#7: Jun 26 '09

re: Converting from ASP to ASP.net


Yeah, you had to move the variables into the correct place to work.
I would still recommend properly using the .vb file associated with the aspx page.
Reply