ASP.Net has me confused, little help please

D

Daniel

I'm new to .Net and all of its abilities so I hope this makes sense.

Basically I'm confused on when is the appropriate time to use web forms
controls vs. regular HTML.

For example in ASP (non-.Net) if I wanted to fill a list it may look
something like this:

-------START CODE
<%
Dim dataObj, arr, rs, hasRecs, i
Set dataObj = Server.CreateObject("MyDll.DataAccess")
Set rs = dataObj.ExecuteQuery("SELECT * FROM SomeTable")
hasRecs = Not rs.EOF
If hasRecs Then arr = rs.GetRows
Set rs = Nothing
%>

<select name="cboSomeList" style="width:200px;" class="comboBox">
<%If hasRecs Then
For i = 0 To UBound(arr, 2)%>
<option value="<%=arr(i, 0)%>><%=arr(i, 1)%>
<%Next
End If%>
</select>
-------END CODE

Pretty straight forward right? In the example above I filled a combo box,
but I could easily change the HTML in the loop to make it a table, or a
series of inputs for a form or whatever.

Now if I wanted to do something similar in ASP.Net, I have some choices to
make.

1) Use some sort of Web Form Control (DataList, DataGrid, Repeater, Table,
ListBox, ComboBox)
- I'm a little confused here because there seem to be numerous controls that
seem to do similar things.
- I can use the DataList (which I have tried to use) however the code is
kinda all over the place.
- I set all it's appropriate properties (I think).
- In the aspx.vb code I got a DataSet (from my new .Net DataAccess dll)
- Created a DataTable and added columns and rows to it
- Created a DataView which added the DataTable
- Finally bound the DataList to that DataView (anyone else find this a
little redundant? shouldn't I be able to just bind it to the DataSet?).
- After all that I noticed that I had to give it an ItemTemplate to use
(That Template Editor makes no sense to me).
- So I looked for examples and found this : <%#
DataBinder.Eval(Container.DataItem, "ColumnName")%>
- However the result is a list with only 1 column.....??? The DataView that
the DataList is supposedly bound to has a DataTable with numerous columns,
what gives?
- It's that damn ItemTemplate I suspect, so what can I do to have multiple
columns?
- It almost looks like I need to concatenate the different columns (with
alignment spacing to format the columns)
- If that's the case what is the point of the DataList? Should I have saved
myself some time and coded it like I did in my example from above (from
non-.Net)?
- Is there any web form control that looks like the ListView from VB 6?
That would be nice.

2) Should I even bother coding the aspx.vb page or just code everything on
the HTML side with <% and %>?
- It looks like the aspx.vb page is good for doing event handling code, but
I'm confused about where to put my initial "page building" code.
- In fact, the DataList examples that I have seen all the coding is done on
the aspx page (which might be so people can copy & paste the example I
suspect).

3) Should I even use the Web Form controls?
- They seem nice but are also kind of restricted (or at least at first
glance).
- If I wanted to create a list from data that has multiple columns and rows
which might have text boxes in certain cells what should I use?
- Can I highlight the row by changing the background color during
ommouseover and onmouseout events with Web Form Controls? How? There are
no <tr>'s to add that code to.


Any help would be appreciated. I basically just want some general
guidelines to follow about when it's appropriate to use Web Form Controls vs
classic ASP style code. In your applications do you normally use these
controls? Is most of your server-side code (or all...) done on the aspx
page or the aspx.vb page?

Thanks,
Dan
 
C

Cowboy \(Gregory A. Beamer\)

Daniel said:
I'm new to .Net and all of its abilities so I hope this makes sense.

Basically I'm confused on when is the appropriate time to use web forms
controls vs. regular HTML.

For example in ASP (non-.Net) if I wanted to fill a list it may look
something like this:

-------START CODE
<%
Dim dataObj, arr, rs, hasRecs, i
Set dataObj = Server.CreateObject("MyDll.DataAccess")
Set rs = dataObj.ExecuteQuery("SELECT * FROM SomeTable")
hasRecs = Not rs.EOF
If hasRecs Then arr = rs.GetRows
Set rs = Nothing
%>

<select name="cboSomeList" style="width:200px;" class="comboBox">
<%If hasRecs Then
For i = 0 To UBound(arr, 2)%>
<option value="<%=arr(i, 0)%>><%=arr(i, 1)%>
<%Next
End If%>
</select>
-------END CODE

Pretty straight forward right? In the example above I filled a combo box,
but I could easily change the HTML in the loop to make it a table, or a
series of inputs for a form or whatever.

Sure, but you end up with a lot of code, right?

In .NET, you simply bind the control to a DataSet. It can be done with drag
and drop, but you learn more with code. Let's go to work:

1. Slap a DropDownList control on your page. It will be called DropDownList1
2. Open up the CodeBehind page and write the following in your Page_Load
event:

'NOTE: Code not tested
If Not (Page.IsPostBack)
Dim conn As New SqlConnection(connStringHere)
Dim cmd As New SqlCommand(SqlString,conn)

Dim dr As New DataReader()

conn.Open()
Dim dr As New DataReader = cmd.ExecuteReader()

DropDownList1.DataSource = dr
DropDownList1.DataValueField = "ID"
DropDownList1.DataTextField = "Description"
DropDownList1.DataBind()

conn.Close()
End If

Less code, and .NET handles the binding for you. It does require a change in
thinking, however.

Now if I wanted to do something similar in ASP.Net, I have some choices to
make.

1) Use some sort of Web Form Control (DataList, DataGrid, Repeater, Table,
ListBox, ComboBox)

Best usage:
DataList - list of items (one column) - can be kludged to do otherwise,
however
DataGrid - spreadsheet, may be sortable, pageable and editable
Repeater - Working with complex HTML
Table - CodeBehind creation of grid, otherwise, set table up in repeater
ListBox - Multiple item selection box
DropDownList - Single item drop down

The binding is fairly similar for each control, except the Repeater, which
is late bound.
- I'm a little confused here because there seem to be numerous controls that
seem to do similar things.
- I can use the DataList (which I have tried to use) however the code is
kinda all over the place.

Shouldn't be, if you are using it correctly. With the above sample, you
simple change it to:

DataList1.DataSource = dr

And bind the columns declaratively in the HTML code in your ASPX page.
- I set all it's appropriate properties (I think).
- In the aspx.vb code I got a DataSet (from my new .Net DataAccess dll)
- Created a DataTable and added columns and rows to it
- Created a DataView which added the DataTable
- Finally bound the DataList to that DataView (anyone else find this a
little redundant? shouldn't I be able to just bind it to the DataSet?).
- After all that I noticed that I had to give it an ItemTemplate to use
(That Template Editor makes no sense to me).
- So I looked for examples and found this : <%#
DataBinder.Eval(Container.DataItem, "ColumnName")%>

Late binding.
- However the result is a list with only 1 column.....??? The DataView that
the DataList is supposedly bound to has a DataTable with numerous columns,
what gives?

DataList has to be kludged to get multiple columns. It is not designed for
multiple columns, but for a single column.
- It's that damn ItemTemplate I suspect, so what can I do to have multiple
columns?

Change to a DataGrid.
- It almost looks like I need to concatenate the different columns (with
alignment spacing to format the columns)
- If that's the case what is the point of the DataList? Should I have saved
myself some time and coded it like I did in my example from above (from
non-.Net)?

DataLists are great for single columns, like a list of hyperlinks.
- Is there any web form control that looks like the ListView from VB 6?
That would be nice.

I forget what ListView looks like (sorry), but you can use a DataGrid for
multiple columns, or use a Repeater for fairly complex HTML output.
2) Should I even bother coding the aspx.vb page or just code everything on
the HTML side with <% and %>?

Depends on the outcome desired. In general, I try to put code in the
CodeBehind page rather than in the ASPX page using <% %>. With late binding,
- It looks like the aspx.vb page is good for doing event handling code, but
I'm confused about where to put my initial "page building" code.

Page_Load event. If you need to dynamically add controls, use a container
object, like a Panel. You can then do something like:

Panel1.Controls.Add(MyControl)
- In fact, the DataList examples that I have seen all the coding is done on
the aspx page (which might be so people can copy & paste the example I
suspect).

Or the fact that the first authors wrote their ASP.NET code like a new form
of ASP code. Most of the early books really sucked, and many people got
their examples from these books.
3) Should I even use the Web Form controls?

I would say yes, if you change your methodology from COM (ASP) to .NET
(ASP.NET), you will see that this is good advice. As long as you are
thinking ASP, the controls are confusing.
- They seem nice but are also kind of restricted (or at least at first
glance).

There are some restrictions, but you can overcome them by writing your own
HTML in a Repeater. You can also create your own classes that are derived
(subclassed) from the controls classes, if you find that they are restricted
too much. Derived classes are outside of your league right now, but you will
get there if you perservere.
- If I wanted to create a list from data that has multiple columns and rows
which might have text boxes in certain cells what should I use?

DataGrid or Repeater. If the person is editing many rows at once, you would
be better suited with a Repeater, as it is more flexible. It is also more
difficult to set up (time consuming might be a better term here).
- Can I highlight the row by changing the background color during
ommouseover and onmouseout events with Web Form Controls? How? There are
no <tr>'s to add that code to.

With a Repeater, you can make the ItemTemplate have your tr tags and give
them an id by concatenating the ID field in your DataTable with a word that
you can uniquely find the TR from. From there it is a matter of matching the
JavaScript with the tag ID.
Any help would be appreciated. I basically just want some general
guidelines to follow about when it's appropriate to use Web Form Controls vs
classic ASP style code. In your applications do you normally use these
controls? Is most of your server-side code (or all...) done on the aspx
page or the aspx.vb page?

Initially, I coded everything by hand. I then switched to C# to get out of
the VB mindset, so I would learn the new paradigm. I now use the controls to
save tons of time. After I learned the methodology, I switched back and
forth from C# to VB. My biggest hurdle was mentally tying the syntax to the
methodology. It sounds like this is a hurdle to you, as well.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
D

Daniel

Thanks for the help.

I'll look into using the Repeater, hopefully that will be able to do what I
need it to do. It sounds like almost all of your server-side coding is done
with the ASPX.VB (or "code-behind") page, which is where I first expected
(and wanted) most code to be. I was getting a little worried as the code
started appearing everywhere for a simple list, but it seems that was
because I was using the wrong Web Form control and trying to bend it to fit
my needs.

Thanks,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top