 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 18th, 2007, 03:37 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Re: ASP DB/RS functions in ASP.NET
Hello,
This may be a silly question, but I've been trying to figure out how to perform a simple query and display in ASP.NET using a HTML page and a Code Behind file.
To clarify what I mean, in Classic ASP, I would have achieved this in the following way.
<% set myRs = objConn.Execute("SELECT * FROM table WHERE something='blah'") %>
Name: <%=myRs("myColumn1")%>
Street: <%=myRs("myColumn2")%>
Now, in ASP.NET I've been reading about all these fancy DataGrid's, DataBinder's, ASP:Repeater's, etc. But I can't seem to find anything to allow me to perform the most basic of tasks being to print out column by column in specific places on a page, rather than as a self contained table, etc.
|
|

May 18th, 2007, 10:21 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You can use the label or literal controls:
<asp:label runat="server" id="lblName" />
<asp:literal runat="server" id="litStreet" />
Then in the code behind, you assign the control's text property with the desired values:
lblName.Text = value;
litStreet.Text = value;
You'll want to learn about the DataReader and DataSet/DataTable classes for getting data.
- Peter
|
|

May 19th, 2007, 08:37 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thanks for your help Peter.
Just wondering if anyone could shed any light on how I'd loop through a couple of records using an ASP:Label or ASP:Literal.
In Classic ASP I would write something like
Code:
<% While Not myRs.EOF %>
... Loop through and display records
<% myRs.MoveNext
Wend %>
Can I do something similar with an ASP:Repeater? Eg
Code:
<ASP:Repeater runat="server">
<ASP:Literal runat="server" id="title1" />
</ASP:Repeater>
|
|

May 20th, 2007, 11:02 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
It's even easier than that. (This will be from memory so pardon the syntax mistakes.)
<asp:repeater id="repeater1">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "fieldname") %>
</itemtemplate>
</asp:repeater>
You can put whatever HTML you want in the item template. There is also a header and footer template as well as as a separator template (so you don't have to do any logic to determine if there are more items like we did in classic ASP). Inside of the item template, you use the data binding code to bring in values.
Take a look at tehe MSDN docs for more specifics on the repeater.
http://msdn2.microsoft.com/en-us/lib....repeater.aspx
- Peter
|
|

May 21st, 2007, 12:16 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Eekk... DataBinder! I guess I can't use ASP:Label or ASP:Literal inside an ASP:Repeater then? I suppose that would make sense since the "ID" is hard coded to a specific name.
I'm sure ASP.NET is much more friendly than Classic ASP... although at the moment I'm struggling to see why. :)
I do like having code separation though, and compiled source code.
Thanks again for your help Peter.
|
|

May 21st, 2007, 08:38 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You most certainly can use just about any server control in a repeating control (Repeater, DataGrid, etc).
<asp:repeater id="repeater1">
<itemtemplate>
<asp:label runat="server" text='<%# DataBinder.Eval(Container.DataItem, "fieldname") %>' />
</itemtemplate>
</asp:repeater>
But in the end, the label just renders a <span> tag while the literal just renders the raw text, so using a label or literal in a repeating template is usually overkill.
If you want to be able to access some element in a repeating template from the code behind, then you must use a server control. I won't bother to show you how to do this quite yet, as it's a bit more complex.
Migrating from ASP to ASP.NET is a big step. You have to change a lot of thinking about how to go about things. There are LOTS of posts to answer about ASP -> ASP.NET so poke around this forum and you should be able to find lots of help.
- Peter
|
|

May 27th, 2007, 07:09 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Eekk... can you believe that I've now mastered the art of looping through database records using ASP:Repeater and Item Templates but can't figure out something as simple as displaying 1 record only?
I have a form in an application I'm building which allows users to modify their submission. I want to pass the results from a database into the value of the form textboxes.
In classic ASP, this is what I would have done.
<form>
<input type="text" name="Name" size="50" value="<%=myRs("Name")%>">
<input type="text" name="Email" size="25" value="<%=myRs("Email")%>">
<input type="text" name="Address" size="30" value="<%=myRs("Address")%>">
<input type="submit" name="submit" value="Submit">
</form>
Now I'm guessing that obviously I just need to replace the <%=myRs("Name")%> with the .Net equivalent <%# DataBinder.Eval(Container.DataItem, "Name") %> although, I don't understand what I need to change in the codebehind because the code I have is for data binding to an ASP:Repeater control.
|
|

May 29th, 2007, 11:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Usually I do this by just working with the single record, and manually assigning the values from it to the controls:
txtName.Text = myRs("Name")
basically something like that.
Add your controls to the page design surface so they will show up in the codebehind, then you can manipulate them as desired.
- Peter
|
|
 |