Write in .aspx page or .aspx.vb(Codebehind)?

Hi, i want to know, if is better to make an entire page in the aspx file or in the aspx.vb file…

if you have to decide, which one do you choose?

ie in aspx:


<asp:Table ID="Table1" runat="server" Width="980">
<asp:TableRow>
<asp:TableCell>
SOME TEXT
</asp:TableCell>
</asp:TableRow>
</asp:Table>

ie in aspx.vb:


dim tabla as new table
tabla.id = "Table1"
tabla.width = 980

dim fila as new tablerow
dim celda as new tablecell

celda.text = "SOME TEXT"
fila.cells.add(celda)
tabla.rows.add(fila)
form1.controls.add(tabla)

thanks!

You do not want to be loading everything from .vb page. The aspx is meant for your layout and presentation, so it should be used as much as possible, without using vb code directly on it. It is to help seperate the code

So what would want to do is declare your table on the aspx, and dynamically bind to it from the vb page. If the table does not need to be dynamic, as above, you can use normal html tables and do it in the aspx page, but I doubt you mean that and the above is just and example.

What type of data you binding to the table? Might be a better option for you to use a GridView or similar control.

I hope this helps

use code behind when you need to create the table at runtime, otherwise you’re fine with the table on the aspx page

:slight_smile: Well, you can’t say you’re not getting plenty of advice. The key thing from my point of view is to use each item for what it’s for. I can identify with Nightstalker’s description of using the .aspx page for structure and presentation while the code behind is for behavior.

However, from a technical standpoint, you can do it either way. It is largely a matter of personal preference. I skew more towards working in code behind than most guys because I like to work programmatically rather than in the attribute based ML language, all other things being equal. Plus, I’m a UX designer and web server controls frequently don’t give me the options that I need to generate the HTML output I’ve developed staticly. It is usually easier to recreate fine-tuned output programmatically. I would say that is the big difference. Typically using the ML based syntax on the .aspx page is faster but less flexible and works well for simple to medium complexity projects (ie .NET has wrapped up a lot of functionality that you can deploy with relatively little work. If you can limit your project scope to things .NET does well or that require relatively modest extensions, then that’s probably faster. However, if you need to do a lot of programmatic work anyway, it’s easier to do what you need with straight up OOP; if it’s specific to this page, I’d write it in the code behind and if you’d like to reuse it from page to page, put it in a .vb file in the App_Code directory, define a namespace in it and reference that namespace in your code behind.

The right option is to use MVC and not Webforms at all. :slight_smile:

Woot! Yeah. :smiley: