Print Page | Close Window

Finally ASP.NET

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=20861
Printed Date: 28 March 2026 at 5:58pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Finally ASP.NET
Posted By: theSCIENTIST
Subject: Finally ASP.NET
Date Posted: 31 July 2006 at 6:41am
Hi guys, I'm finaly asp.neting (VB so no weird voids) and I'm finding the whole transition stimulating, but there's a few things that I'm having a hard time adjusting, here's a few:

Subs and functions no longer go inside <% ... %> delimiters but outside of it wrapped by the <script> tag ???

This one alone took me a few hours to bypass yesterday, who would have though!

I have a [ Dim thisVar ] and thisVar is not accessible inside my Subs and Functions on the same file, I tried Public thisVar and Static thisVar, still no access to it.

Can anyone tell me how?


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com



Replies:
Posted By: VBScript
Date Posted: 31 July 2006 at 7:56am
With ASP .Net ever block of code inside the <% %> tags are treated as individual private subs.

Try declaring the vars in an include.


-------------
http://www.james-r.co.uk" rel="no follow - James
http://www.gotrillian.com/?4498-20" rel="no follow - Trillian - M


Posted By: michael
Date Posted: 31 July 2006 at 3:02pm
Include? There is no real need for includes in asp.net (besides inheritance)

If you want a variable accessible outside of its own block you need to declare it outside as well. So for example:


Private var1 as string
Private var2 as string

Private Function Page_Load(...)
If not IsPostback THEN
    var1="Foo"
    var2="Bar"
End If
End Function

Friend Function world() as string
Return foo & bar
End Function


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date Posted: 31 July 2006 at 3:05pm
Or of course, forgot to mention you can pass the var as ByRef i.e.

Private Function foo()
Dim var1 as string = "bar"
doSomethingWithThatVar(var1)
End Function

Private Function doSomethingWithThatVar(ByRef x as string) as string
x=x.Substring(x,1)
End Function

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: theSCIENTIST
Date Posted: 01 August 2006 at 9:46am
Is it just me or in ASP.NET you can actually get away with not using Response.Write for everything and not using <% ... %> blocks?
 
Observe this file:
 

<html>
... html ...
</html>
<script>
... asp.net ...
</script>
 
No <% ... %> blocks and no Response.Writes for every HTML line, is this a good way to code in ASP.NET? Or the <script> block goes before the HTML so stuff can be available there when needed?
 
Sorry, but I'm just starting, and this is so different then ASP.


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: Mart
Date Posted: 01 August 2006 at 10:58am
Only poorly written ASP.NET software will use Response.Write, actually scratch that, because it can be the only option in some cases (for instance if you're writing a http module).

The <%# tag is needed for databinding, so you can't really get away without using it, but theres no need whatsoever to put the page logic in those blocks.

The de-facto way to structure an ASP.NET page is to have the page logic in a seperate file to the HTML, which is quite confusing to get used to at first, but once you get used to object orientation it seems like the right thing.

The idea is that if you wanted a simple page that regurgitated the users name when they press submit, you would structure it like this:

WebForm.aspx:

<%@ Page Language="C#" %>
<html>
...
<asp:Label id="lblName" runat="server"></asp:Label>
<p>
Full Name: <asp:TextBox id="txtName" runat="server"></asp:TextBox>
<asp:Button id="submit" runat="server">Submit</asp:Button>
</p>
..
</html>

WebForm.aspx.cs

using System;
using System.Web;

public class WebForm
{
    //visual studio generated code here

    //this is called automatically when the button is set
    public void submit_click(object sender, EventArgs e)
    {
       //set the text of the label to the value of the text box
       lblName.Text = txtName.Text;
    }
}



Posted By: MadDog
Date Posted: 01 August 2006 at 10:59am
You can put ASP.Net code inside <script> tags if you add runat="server" in the script tag.... anyways its something like that. Its been a while since i messed around with ASP.Net and i cant remember!


-------------
http://www.iportalx.net" rel="nofollow">


Posted By: theSCIENTIST
Date Posted: 01 August 2006 at 11:49am
Mart, you have two files there, but I don't see where you call/include them.
 
I also notice you use: [ <asp:Label id="lblName" runat="server"></asp:Label> ] when I use [ <asp:Label id="lblName" runat="server" /> ] this will create a normal [ <span>...</span> ] do the labels need to be closed like you do?
 
Maddog I figured now how to do this, all code goes inside <script> and we only use <%= ... %> in the HTML to write some value. Thanks.


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: Mart
Date Posted: 01 August 2006 at 2:06pm
The easiest way to reference the files together is to use code behind in Visual Web Developer 2005 (it's free), that way you don't have to manually compile files etc.
I find it hard to describe how to architect asp.net apps in forum posts, the best thing to do is to either grab the source code of a small asp.net project or get a book and work through it (or both)


Posted By: michael
Date Posted: 01 August 2006 at 4:51pm
There is no include necessary.
Essentially you have i.e.
Default.aspx
   Default.aspx references (this ex. is 2.0)
   CodeFile="Default.aspx.cs"
        Inherits="Default"

this info is in the header of the file so it automatically knows to use Default.aspx.cs to compile.... Furthermore there may be a default.resx file which contains the resources (labels etc) for that file.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net