| Author |
Topic Search Topic Options
|
theSCIENTIST
Senior Member
Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
|
Post Options
Thanks(0)
Quote Reply
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?
|
|
|
 |
VBScript
Senior Member
Joined: 14 July 2004
Location: United Kingdom
Status: Offline
Points: 219
|
Post Options
Thanks(0)
Quote Reply
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.
|
|
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
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
|
|
|
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
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
|
|
|
 |
theSCIENTIST
Senior Member
Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
|
Post Options
Thanks(0)
Quote Reply
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.
|
|
|
 |
Mart
Senior Member
Joined: 30 November 2002
Status: Offline
Points: 2304
|
Post Options
Thanks(0)
Quote Reply
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; } }
|
 |
MadDog
Mod Builder Group
Joined: 01 January 2002
Status: Offline
Points: 3008
|
Post Options
Thanks(0)
Quote Reply
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!
|
|
|
 |
theSCIENTIST
Senior Member
Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
|
Post Options
Thanks(0)
Quote Reply
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.
|
|
|
 |