Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
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
 
Old May 18th, 2007, 03:37 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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.
 
Old May 18th, 2007, 10:21 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old May 19th, 2007, 08:37 PM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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>
 
Old May 20th, 2007, 11:02 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old May 21st, 2007, 12:16 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.
 
Old May 21st, 2007, 08:38 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old May 27th, 2007, 07:09 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.
 
Old May 29th, 2007, 11:00 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.Net, Access DB, and a webform? TheNinthPlayer BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 8 June 3rd, 2008 10:28 AM
calling .jsp functions from ASp.net Laxmikant_it .NET Framework 2.0 2 January 1st, 2008 02:47 AM
rs.filter in asp.net - Please help ivanv ASP.NET 2.0 Basics 0 May 13th, 2007 05:14 AM
API functions in ASP.NET 1.1 zuesegy ASP.NET 1.0 and 1.1 Basics 0 June 11th, 2006 09:09 AM
Intro to ASP.NET 2.0 DB mar0364 ASP.NET 1.0 and 1.1 Basics 1 April 9th, 2005 11:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.