ASP.NET programmers, help me out here.

notfred

Lifer
Feb 12, 2001
38,241
4
0
Ok, I'm trying to learn ASP.NET, but the whole programming model isn't making sense to me.
You're suppsoed to be able to seperate design and code with the code-behind model, right?

I'm used to doing web development with perl, let me write a pseudocode example of what I'm familiar with:


print "<html><head></head><body>";

if ( user is logged in )
{
body();
}
else
{
print "Sorry, you're not logged in";
}

print "</body></html>";

function body()
{
print "You're logged in, here's a table: <br><table>";
for ( count = 0; count < 10; count++ )
{
print "<tr><td>This is line number", count, "</td></tr>";
}
print "</table>";
}


Ok, so that's pretty straightforward code I think. HOw do I do that while seperating a page into two files? What goes in the .aspx file, since essentially the entire page displaying is conditional based on the user being logged in.

Shouldn't I not be creating html code in my C# file? Am I expecting too much here, or will the asp.net application end up looknig a lot like the perl application?

Anyone have any insight? How would you seperate this into two files as a more experienced ASP.NET developer?
 

royaldank

Diamond Member
Apr 19, 2001
5,440
0
0
You can have all the info in one file.

But, you're going to see that you don't need to do tables like that anymore. If you're an ASP programmer your going to see that it's Object Oriented now and a whole different ball game. For example, here you'd have a datagrid on the page. When the page is loaded, it would check some variables to see if someone is logged in, and then execute it's search if they are.

I know this is just an example. From what you put, it looks like you don't have a solid understanding of how .NET works in relation to how ASP or other scripting languages work. It's more like writing a standalone windows app where the interface is a webpage.

 

torpid

Lifer
Sep 14, 2003
11,631
11
76
Oh boy. Well, this is a fairly simple example, but if you want to get at a more "serious" way of checking if the user is logged in, that's a different subject.

In this case, you have a few options. One is to have a server side label and a repeater. If the user is logged in, the label will read: "You're logged in" and then you'd bind data to the repeater. The ASPX page will have the presentation logic for the repeater.

A different, possibly better approach, again assuming you really want this on one page, is to have two panels on the page, one with a message about not being logged in, and the other with a message that you are logged in and a repeater.

If you want to avoid a repeater, you can always use a StringBuilder to create the iterated string for this is line number x.

In a more serious architecture, your user would never get to the page, because your authorization / authentication mechanisms would prevent it.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: royaldank
I know this is just an example. From what you put, it looks like you don't have a solid understanding of how .NET works in relation to how ASP or other scripting languages work.

If I knew how it worked I wouldn't need to ask questions about it.
 

royaldank

Diamond Member
Apr 19, 2001
5,440
0
0
Originally posted by: notfred
Originally posted by: royaldank
I know this is just an example. From what you put, it looks like you don't have a solid understanding of how .NET works in relation to how ASP or other scripting languages work.

If I knew how it worked I wouldn't need to ask questions about it.

Wasn't sure if you were asking to get something done or trying to start learning it. Sorry for trying to help.
 

Tea Bag

Golden Member
Sep 11, 2004
1,575
3
0
I'm not real proficient with it myself yet but:

the code-behind feature of ASP.NET divides the ASP.NET Web page into two files, with one file containing the presentation content and the other file, called the code-behind file, containing the application logic.

The presentation content is stored as a file with a .aspx extension (the html), while the code for the application logic is stored in a file with a .cs extension.

The following code (should be) present in the .aspx file by default:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="MyApp.WebForm1"%>


* The AutoEventWireUp attribute specifies whether the Visual Studio .NET Framework automatically calls page events and methods. By default, the AutoEventWireUp attribute is set to false specifying that code for handling page events needs to be written explicitly.

* The Codebehind attribute specifies the code-behind file for the Web page.

* The Inherits attribute specifies the class in the code-behind file from which the Web page inherits.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: royaldank
Originally posted by: notfred
Originally posted by: royaldank
I know this is just an example. From what you put, it looks like you don't have a solid understanding of how .NET works in relation to how ASP or other scripting languages work.

If I knew how it worked I wouldn't need to ask questions about it.

Wasn't sure if you were asking to get something done or trying to start learning it. Sorry for trying to help.

I'm just trying to learn the fundamentals of .NET, I only included my pseudocode as an example of the method I'm familiar with, I'm trying to figure out how that type of scenario should be handled in .NET.

You mentioned a datagrid, and I found it in the toolbox in Visual Studio, and I can figure out how to use it from there, but I never would have known that there was such a thing as a DataGrid object if you hadn't mentioned it. Similarly, I have no idea what the "proper" method for authenticating users in .NET is, and I'm really not familar with very much of .NET at all at this point.
 

royaldank

Diamond Member
Apr 19, 2001
5,440
0
0
ADO.NET and ASP.NET are real beasts if you're used to scripted languages. I still haven't gotten any good at .NET. I've tried a couple different projects so far, get behind, and then end up doing it in regular ASP because I'm running out of time.

There are some free Microsoft Computer Based Learning videos available. I just got them the other day and have only watched a couple minutes. They look pretty good though and discuss a lot of good info.

Here's the link:
ASP.NET Webcasts
 

replicator

Senior member
Oct 7, 2003
431
0
0
Simple hello world tutorial using Web Matrix, that uses a script block in the aspx page, but can easily be done using code behind with a cut and paste.

http://www.asp.net/webmatrix/t.../addbasiccontrols.aspx

Generally, it is bad practice to put HTML code within your code behind file, (separation of Presentation and UI) so rarely will you ever see anything that is comparable to that perl code above in ASP .NET.
You might want to first read up on Object Oriented programming before jumping into asp .net, if you are new to it.
 

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
are you using an IDE like vs.net or webmatrix? I would reccomend you do since it simplifies the process of having code-behind. web matrix is free from asp.net and the getting started tutorials should help you learn to use it.

But for what you're trying to do, basically add an asp:label to your aspx page and a datagrid or repeater. then in your code behind you need a page load:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

if (user is logged in)
label.text = "You are logged in"
datagrid.datasource = whatever you need for this
datagrid.databind() 'fills the datagrid and creates the table
else
label.text = "You are not logged in"
datagrid.visible = false
end if

end sub

the pageload function is called when the page is first loaded by the browser. this way the label changes based on whether or not the user is logged in (I use session since I don't know of any other way, I'm still learning too!). also, if the user is not logged in, the datagrid will not be visible!

hope this helps!!

edit: oh yeah forgot to mention that in the code above, "label" and "datagrid" should be changed to whatever your id for each control is.
 

HJB417

Senior member
Dec 31, 2000
763
0
0
Originally posted by: notfred
Ok, I'm trying to learn ASP.NET, but the whole programming model isn't making sense to me.
You're suppsoed to be able to seperate design and code with the code-behind model, right?

Code behind blows as it's used only for vs.net, just remove the "codebehind" directive, and keep the "inherits" directive. That's what you really want. The only disadvantage is, .net won't compile the cs file as it's done @ runtime by asp.net.

 

mAdMaLuDaWg

Platinum Member
Feb 15, 2003
2,437
1
0
Originally posted by: HJB417
Originally posted by: notfred
Ok, I'm trying to learn ASP.NET, but the whole programming model isn't making sense to me.
You're suppsoed to be able to seperate design and code with the code-behind model, right?

Code behind blows as it's used only for vs.net, just remove the "codebehind" directive, and keep the "inherits" directive. That's what you really want. The only disadvantage is, .net won't compile the cs file as it's done @ runtime by asp.net.

Ummm... the codebehind directive doesn't do anything. Its not processed by the ASP.NET compiler... its just there as a convenience for the programmer to remind him of what file the code is in. The Inherits keyword is what directs the codebehind. Therefore if you remove the codebehind directive its not going to have any effect
 

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
codebehind is an excellent way to separate the desing from the implementation. I find it very useful and keeps things neat. to each their own however and if you don't like it, it's designed to do both. but keeping all your code in a separate file makes updates very easy!

btw those webcasts are great, I saw them and got the free book too :thumbsup:
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: HJB417
Originally posted by: notfred
Ok, I'm trying to learn ASP.NET, but the whole programming model isn't making sense to me.
You're suppsoed to be able to seperate design and code with the code-behind model, right?

Code behind blows as it's used only for vs.net, just remove the "codebehind" directive, and keep the "inherits" directive. That's what you really want. The only disadvantage is, .net won't compile the cs file as it's done @ runtime by asp.net.

I'm using vs.net.
 

HJB417

Senior member
Dec 31, 2000
763
0
0
Originally posted by: SelArom
codebehind is an excellent way to separate the desing from the implementation. I find it very useful and keeps things neat. to each their own however and if you don't like it, it's designed to do both. but keeping all your code in a separate file makes updates very easy!

btw those webcasts are great, I saw them and got the free book too :thumbsup:

the inherits page directive is an excellent way to separate the desing from the implementation. I find it very useful and keeps things neat. to each their own however and if you don't like it, it's designed to do both. but keeping all your code in a separate file makes updates very easy!

btw those webcasts are great, I saw them and got the free book too :thumbsup: