Converting from ASP to ASP.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • programmerboy
    New Member
    • Jul 2007
    • 84

    Converting from ASP to ASP.net

    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.

    Code:
    <div id="buttons">
    <ul id="topImages" class="menu" style="margin-left: -2px;">
    <li>
    <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>
    <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" >
    </li>
    </ul></div>
    and if my home page I have some code like this.

    Code:
    <%@ Register TagPrefix="uc" TagName="topMenu" Src="~/inc/topMenu.ascx" %>
      
    <%
        Dim home, services As String
    
        home = "_selected"
        services = ""
    %>
    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
    Last edited by Frinavale; Jun 26 '09, 04:34 PM. Reason: Moved to ASP.NET from .NET
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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:
    Code:
    <%@ 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:
    Code:
    <%@ Page Language="VB" %>
    <script runat="server">
        Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs)
            Label1.Text = "Server click handler called."
        End Sub
    </script>
    
    <body>
      <form id="form1" runat="server">
        <asp:Button ID="Button1" Runat="server" 
          OnClick="Button1_Click" 
            OnClientClick="return confirm('Ready to submit.')" 
            Text="Test Client Click" />
        <br />
        <asp:Label ID="Label1" Runat="server" text="" />
      </form>
    </body>
    </html>
    Just post your first line and we'll be able to help you better.

    -Frinny

    Comment

    • programmerboy
      New Member
      • Jul 2007
      • 84

      #3
      This is the default.aspx code

      Code:
      <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
      <%@ Register TagPrefix="uc" TagName="homeText" Src="~/inc/homeText.ascx" %>
      <%@ Register TagPrefix="uc" TagName="pageHeader" Src="~/inc/header.ascx" %>
      <%@ Register TagPrefix="uc" TagName="leftMenu" Src="~/inc/leftMenu.ascx" %>
      <%@ Register TagPrefix="uc" TagName="footer" Src="~/inc/footer.ascx" %>
      <%@ Register TagPrefix="uc" TagName="headTags" Src="~/inc/headTags.ascx" %>
      <%@ Register TagPrefix="uc" TagName="topMenu" Src="~/inc/topMenu.ascx" %>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      <title>Omair Tech &amp; Designs</title>
      <uc:headTags ID="headTags" runat="server" />
      </head>
      
      <%
          Dim home, solutions, services, support, about, contact As String
      
          home = "_selected"
          solutions = ""
          services = ""
          support = ""
          about = ""
          contact = ""
      %>
      
      <body>
      <form id="userControlForm" runat="server">
      <table id="mainTable" align="center" border="0">
      	<tbody>
      	<tr>
      		<td>
      		<uc:pageHeader ID="pageHeader" runat="server" />
      		<uc:topMenu id="topMenu" runat="server" />
      		
      		<table id="mainContainer" border="0" cellspacing="0px">
      			<tbody>
      			<tr>
      				<!-- Left Menu -->
      				<uc:leftMenu ID="leftMenu" runat="server" />
      
      				<td id="mainContainerTextArea" valign="top">
      				    <uc:homeText id="homeText" runat="server" />
      				
      				</td>
      			</tr>
      		</tbody>
      		</table>
      		</td>
      	</tr>
      	<uc:footer ID="footer" runat="server" />
      </tbody>
      </table>
      </form>
      </body>
      </html>
      And this is the topMenu.ascx code. Every user control is in "inc" folder.

      Code:
      <%@ Control Language="VB" AutoEventWireup="false" CodeFile="topMenu.ascx.vb" Inherits="inc_topMenu" %>
      
      <div id="buttons">
      	<ul id="topImages" class="menu" style="margin-left: -2px;">
      		<li>
      			<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" >
      		</li>
      		<li>
      			<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" >
      		</li>
      		<li>
      			<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" >
      		</li>
      		<li>
      			<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" >
      		</li>
      		<li>
      			<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" >
      		</li>
      		<li>
      			<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" >
      		</li>
      	</ul>
      </div>

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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:
        Code:
            Dim home, solutions, services, support, about, contact As String
         
            home = "_selected"
            solutions = ""
            services = ""
            support = ""
            about = ""
            contact = ""
        I would recommend using proper scope for your variables. So, in your Default.aspx.vb file you should have something like:
        Code:
        Partial Public Class _Default
            Inherits System.Web.UI.Page
        
            Private solutions, services, support, about, contact As String
            Private home As String = "_selected"
        
            Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
            End Sub
        End Class

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          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.

          Comment

          • programmerboy
            New Member
            • Jul 2007
            • 84

            #6
            Actually I was able to fix it using following way

            Code:
            <%@ Control Language="VB" AutoEventWireup="false" CodeFile="topMenu.ascx.vb" Inherits="inc_topMenu" %>
            
            <%
            
                Dim home, solutions, services, support, about, contact, selectedPage As String
            
                selectedPage = Request.Url.ToString
                selectedPage = selectedPage.Substring(selectedPage.LastIndexOf("/") + 1)
                
                Select Case selectedPage
                    Case "default.aspx"
                        home = "_selected"
                    Case "solutions.aspx"
                        solutions = "_selected"
                    Case "services.aspx"
                        services = "_selected"
                    Case "support.aspx"
                        support = "_selected"
                    Case "about.aspx"
                        about = "_selected"
                    Case "contact.aspx"
                        contact = "_selected"
                End Select
            %>
            
            <div id="buttons">
            	<ul id="topImages" class="menu" style="margin-left: -2px;">
            		<li>
            			<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" >
            		</li>
            		<li>
            			<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" >
            		</li>
            		<li>
            			<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" >
            		</li>
            		<li>
            			<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" >
            		</li>
            		<li>
            			<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" >
            		</li>
            		<li>
            			<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" >
            		</li>
            	</ul>
            </div>

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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.

              Comment

              Working...