SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: asp to aspx

Hybrid View

  1. #1
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    asp to aspx

    Please could someone show me how to convert the 3 Classic ASP scripts I posted here http://www.sitepoint.com/forums/show...=1#post5162673 so I can use the .aspx extension and the automatic compiling?

    It hopefully will kick-start my enthusiasm for .NET

    As soon as a put in a function to set the h1, I get the error "Statement cannot appear within a method body. End of method assumed"
    Code:
    <%
    Response.ContentType="application/x-javascript"
    Response.AddHeader("Content-Type", "application/x-javascript")
    dim H1
    function getH1()
     H1="db Loaded"
    end function
    %>o=document.createElement("<h1>");
    o.innerHTML="<%=H1%>";
    document.getElementsByTagName("div")[0].appendChild(o);
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  2. #2
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Mark, I've reviewed the original thread and have a few comments for you.

    First, a direct converson between the two is not possible due to several reasons. These reasons would be a difference in the underlying technology and the fact that vbscript (used in the InterDev days) is no longer used.

    Second, rather than convert your script, please allow me to show you how to do this conversion. Then you can learn more along the way, as you convert your scripts yourself. It's always best to learn by doing.

    I'll assume you have something akin to the following:

    Code ASP:
    <!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>
    <title>My Page</title>
    <%
     
    sub GetHeaderText()
     
     
        GetHeaderText = "Howdy all"
     
    end sub
     
    %>
    </head>
    <body>
     
    <h1><%Response.Write(GetHeaderText()) %></h1>
     
    </body>
    </html>

    And that it is giving you the "cannot exist within a method" error, correct? The reason for this is because asp.net is compiled, managed code. It is just as much a full project as when you would write a custom DLL in VB to provide DAL services for your asp.classic application. In this case, we're trying to add the method GetHeaderText to a global namespace, which isn't allowed. Therefore, we have a different way of handling this.

    To make the conversion, we add the following to a asp.net web application project...

    Code HTML4Strict:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Conversion.Default" %>
    <!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></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
     
            <h1 id="MyHeader" runat="server" onload="MyHeader_OnLoad"></h1>
     
        </div>
        </form>
    </body>
    </html>

    Code Csharp:
    using System;
    using System.Web.UI;
     
    namespace Conversion
    {
     
        public partial class Default : Page
        {
     
            protected void Page_Load(object sender, EventArgs e)
            {
     
                Title = "My Page";
     
            }
     
            protected void MyHeader_OnLoad(object sender, EventArgs e)
            {
     
                MyHeader.InnerHtml = "Howdy all";
     
            }
     
        }
     
    }

    Now the file runs correctly. All we've done is wrapped the base functionality of the entire page in a class, which exposes methods the client html can hook into. You do not, in fact, need to resort to using all the nifty built-in <asp:whatever> controls to use aspx. These controls have a lot more options and settings, and are far more customizable than stock html elements, but you can do the same things with normal html. All that's required is that you give the element id and runat attributes, and map your events to handlers. One of the biggest advantages of this is that we can now use inheritance to create a base page handling class, subclassing it for specific page usage. Also note the first line in the new html. There are directives there that tell the compiler which language you'll be using, which file has the code (no need for SSI anymore, yay), and what namespace.class to use.

    I hope this simple example helps clarify what needs to be done to convert your scripts.

    In addition, you may still use javacript, AJAX, and anything else you want in the HTML portions of your page.

    I realise you are hesitant about .net, and I understand why. It's a bit of a shift in thought. But you can not easily take a Chevy (your asp.classic app) and toss a Ford engine (.net) into it and expect it to work without reworking a few things. Might I suggest that you simply take a leap of faith? Download and install the free MS Web Express IDE, try authoring a few pages to get the feel for it, and then try the conversion. At worst, you've lost a day or two of time exploring things, but you'll have a much better grasp on how it all works, in order to place future queries.

    Happy coding. =)
    Last edited by Serenarules; Aug 9, 2012 at 16:25. Reason: fixed typos

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks Serenarules. How would I go about changing the output from HTML to javascript depending on how it was called?
    At the moment I use default.asp to output HTML as HTML and default.asp?live=1 to output HTML as JavaScript

    What I need is default.aspx to output HTML, and default.aspx?live=1 to output javascript. Using the method I get my asp and php pages to finish executing in over twice the speed.

    I don't want to have to create seperate files that do the same things just in a different way.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Add the following element to your aspx html.

    Code ASP:
    <asp:Literal id="DynamicOutput" runat="server" onload="DynamicOutput_OnLoad" />

    Add the following code to your page class.

    Code Csharp:
     
            protected void DynamicOutput_OnLoad(object sender, EventArgs e)
            {
     
                    if (Request.QueryString["live"] == "1")
                    {
                                    DynamicOutput.Text = "some js here";
                    } else {
                                    DynamicOutput.Text = "some text here";
                    }
     
            }

    The asp:literal control is a custom element in the asp namespace. It is tied to a specialized control class, and therefore, has more properties and methods than a normal html element would. If you use the IDE, you'll see this for yourself, in the intellisense. You could use any number of elements for the output, or handle it in any number of methods, such as Page_Load, but that's not good practice, as that method can quickly become overrun with noodle code.

    There are other ways to accomplish this as well, but until you are comfortable with the basics, this will suffice.

    P.S. If you're more comfortable working within the HTML, you could also forego the code and add the if-else statement directly to the markup, within <% %> tags. The reason I favor the method I showed is that it separates the display from the code.

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I've managed to get something working!
    Code:
    <%@ Page Language="C#" %> 
    <script runat="server"> 
        void Page_PreInit(object sender, EventArgs e)  
        { 
            if (Request.QueryString["live"] == "1")
      {
       Response.Write("javascript header");
      } else {
       Response.Write("HTML header");
      }
        } 
     
        protected override void OnPreInit(EventArgs e) 
        { 
            base.OnPreInit(e); // implicitly calls Page_PreInit 
            if (Request.QueryString["live"] == "1")
      {
       Response.Write("javascript footer");
      } else {
       Response.Write("HTML footer");
      }
        } 
    </script>
    But not sure what it's doing. I don't know if I like this code-behind thing (not that this is using any, I don't think). Over the years I've created my own code and libraries, maybe they're not good but I know what each do, and know how to change them to fit my needs. My PHP and asp are very similar line-by-line (or block by block). But I like the way my includes are compiled, and I have to move to aspx to keep up to date.

    Why do I need page_preinit(argument 1,argument 2). I know what javascript's function init(argument 1,argument 2) would do, but I don't call page_preinit or give it arguments?

    I can remove object sender, EventArgs e from page_preinit and it still works, but if I remove EventArgs e from onPreInit or e from base.onPreInit it fails
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  6. #6
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Well, if you insist on doing it inline, you could probably get away with...

    Code ASP:
    <script runat="server"> 
            if (Request.QueryString["live"] == "1")
      {
       Response.Write("javascript header");
      } else {
       Response.Write("HTML header");
      }
     
    </script>

    ...however I have never tried this myself, so it might not actually work. You also scrap the ability to reuse the code for another page. With aspx you are now working against an object model, not just handling html events. When it comes to includes, the need is eliminated due to how code is namespaced. To use another code file within the one you're working on, you would type "use MyNamespace.MyClass" at the beginning of the file. As you get more advanced, you can learn how to code a custom control, and drop that into your html forms. These are the basis for inclusion in asp.net. When you author html and code separately, the code files are all compiled into a dll in the projects bin folder. This is just another reason/benefit for separating the two.

    I would highly suggest picking up a beginners book on the topic and work through the tutorials. There is more to cover than can easily be done in this thread alone.

    On a personal note: I too fought against the design for a while. There are legitamate reasons why it is put together the way it is, and everything you want to do can be done. But if you fight against it, those things will just seem a lot harder than they really are.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •