Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Finally ASP.NET
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Finally ASP.NET

 Post Reply Post Reply Page  12>
Author
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Topic: Finally ASP.NET
    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?
Back to Top
VBScript View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14 July 2004
Location: United Kingdom
Status: Offline
Points: 219
Post Options Post Options   Thanks (0) Thanks(0)   Quote VBScript Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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;
    }
}

Back to Top
MadDog View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 01 January 2002
Status: Offline
Points: 3008
Post Options Post Options   Thanks (0) Thanks(0)   Quote MadDog Quote  Post ReplyReply Direct Link To This Post 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!
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.