 |
| ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 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
|
|
|
|

March 29th, 2009, 05:08 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
nTier example... how to fill form
Hi! I've downloaded great example of Imar Spaanjaar.
I would like to know how to fill my form (I don't want to use .NET components like formviewer...), I've created single objects in my form: textbox, ddl, checkbox.... Now I would like to know how to fill an entity's istance with a record and how to fill each object of the form (textbox, ddl...)
Can you help me with an easy example?
Thanks
|
|

March 29th, 2009, 06:04 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Hi il_dandi,
Out of interest, is there a particular reason why you don't want to use the built-in controls? FormViews are very handy for linking the page controls to your business tier methods.
If you don't want to use the .NET controls, you can still fairly easily set and retrieve values in the form manually. Here's a simple example you can copy on to a site and run. The core things to look at are the page and button events
Entity code (I've created Load and Save methods to simulate database access)
vbnet Code:
Imports Microsoft.VisualBasic
Namespace BLL
' Entity class Public Class Person Public Name As String Public Gender As String Public SendSpam As Boolean
' represents our "database", as is stored between requests Shared dbPerson As Person
Shared Sub New() dbPerson = New Person() dbPerson.Name = "John Doe" dbPerson.Gender = "M" dbPerson.SendSpam = True End Sub
Public Sub New() End Sub
Public Shared Function Load() As Person ' get from db Return dbPerson End Function
Public Shared Sub Save(ByVal p As Person) ' save to db dbPerson = p End Sub End Class
End Namespace
Page code
vbnet Code:
<%@ Page Language= "VB" %> <%@ Import Namespace= "BLL" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat= "server"> Public Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs ) _ Handles Me. Load If Not Page. IsPostBack Then ' load in details (normally filtered using Request.QueryString or the like) Dim p As Person = Person. Load() ' fill in form NameValue. Text = p. Name GenderValue. SelectedValue = p. Gender SpamValue. Checked = p. SendSpam End If End Sub Public Sub SaveButton_Click (ByVal sender As Object, ByVal e As EventArgs ) _ Handles SaveButton. Click ' read from form Dim p As New Person () p. Name = NameValue. Text p. Gender = GenderValue. SelectedValue p. SendSpam = SpamValue. Checked ' save details Person. Save(p ) End Sub</script> <html xmlns= "http://www.w3.org/1999/xhtml" > <head runat= "server"> <title>Untitled Page</title> </head> <body> <form id= "form1" runat= "server"> <div> <asp:Label runat= "server" ID= "NameLabel" Text= "Name" AssociatedControlID= "NameValue" /> <asp:TextBox runat= "server" ID= "NameValue" /> </div> <div> <asp:Label runat= "server" ID= "GenderLabel" Text= "Gender" AssociatedControlID= "GenderValue" /> <asp:DropDownList runat= "server" ID= "GenderValue"> <asp:ListItem Value= "M" Text= "Male" /> <asp:ListItem Value= "F" Text= "Female" /> </asp:DropDownList> </div> <div> <asp:CheckBox runat= "server" ID= "SpamValue" Text= "Send me Spam?" Checked= "true" /> </div> <div> <asp:Button runat= "server" ID= "SaveButton" Text= "Save" /> </div> </form> </body> </html>
HTH
Phil
|
|

March 30th, 2009, 05:26 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for help!
I've only one question
your BLL is a demo of what I've already in my nTier example??
about Page Code
If Not Page.IsPostBack Then
' load in details (normally filtered using Request.QueryString or the like)
Dim p As Person = Person.Load()
' fill in form
NameValue.Text = p.Name
GenderValue.SelectedValue = p.Gender
SpamValue.Checked = p.SendSpam
End If
Can you tell mw how Can I see the same example using C# code?
|
|

March 31st, 2009, 05:47 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Is it really the same as Imar's example? Wow, I just made it up.
This is the whole thing in C#
Entity code
c# Code:
namespace BLL {
// Entity class public class Person { public string Name; public string Gender; public bool SendSpam;
// represents our "database", as is stored between requests private static Person dbPerson;
static Person() { Person dbPerson = new Person(); dbPerson.Name = "John Doe"; dbPerson.Gender = "M"; dbPerson.SendSpam = true; }
public Person() { }
public static Person Load() { // get from db return dbPerson; }
public static void Save(Person p) { // save to db dbPerson = p; } } }
Page Code
c# Code:
<%@ Page Language="C#" %> <%@ Import Namespace="BLL" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
public override void OnLoad(object sender, EventArgs e) { if(Page.IsPostBack) { // load in details (normally filtered using Request.QueryString or the like) Person p = Person.Load(); // fill in form NameValue.Text = p.Name; GenderValue.SelectedValue = p.Gender; SpamValue.Checked = p.SendSpam; } } public void SaveButton_Click(object sender, EventArgs e) { // read from form Person p = new Person(); p.Name = NameValue.Text; p.Gender = GenderValue.SelectedValue; p.SendSpam = SpamValue.Checked; // save details Person.Save(p); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server">
<div> <asp:Label runat="server" ID="NameLabel" Text="Name" AssociatedControlID="NameValue" /> <asp:TextBox runat="server" ID="NameValue" /> </div>
<div> <asp:Label runat="server" ID="GenderLabel" Text="Gender" AssociatedControlID="GenderValue" /> <asp:DropDownList runat="server" ID="GenderValue"> <asp:ListItem Value="M" Text="Male" /> <asp:ListItem Value="F" Text="Female" /> </asp:DropDownList> </div>
<div> <asp:CheckBox runat="server" ID="SpamValue" Text="Send me Spam?" Checked="true" /> </div>
<div> <asp:Button runat="server" ID="SaveButton" Text="Save" OnClick="SaveButton_Click" /> </div>
</form> </body> </html>
HTH
Phil
Last edited by philip_cole; March 31st, 2009 at 05:49 AM..
|
|

April 2nd, 2009, 10:38 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for help me!
I've this problem
in BBL:
using Spaanjaars.ContactManager.BusinessEntities;
using Spaanjaars.ContactManager.Dal;
using Spaanjaars.Validation;
namespace Spaanjaars.ContactManager.Bll
{
[DataObjectAttribute()]
public static class ContrattoManager
{
#region Public Methods
public static ContrattoCollection GetList()
{
return GetList(string.Empty, -1, -1);
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static ContrattoCollection GetList(string sortExpression)
{
return GetList(sortExpression, -1, -1);
}
..
Business Entity:
Namespace Spaanjaars.ContactManager.BusinessEntities
{
[DebuggerDisplay("Contratto: {CodiceIndicizzazione, nq} {Va, nq} {Decorrenza, nq} {Scadenza, nq}")]
public class Contratto : BusinessBase
{
public Contratto()
{
}
[DataObjectFieldAttribute(true, true, false)]
public override int Id { get; set; }
...
How Can I in my form fill Contratto entity with the values of a specified record?
Thanks
|
|

April 2nd, 2009, 02:50 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
What exactly is the problem? What do you mean with
Quote:
|
How Can I in my form fill Contratto entity with the values of a specified record?
|
Imar
|
|

April 2nd, 2009, 06:37 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've my BLL and my Entity
now I would like to fill my istance for use commands like
myistance.firstname
myistance.lastname
for fill my objects
tbtest.text = myistance.firstname
tbtest1.text = myistance.lastname
Thanks
|
|

April 2nd, 2009, 07:02 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
If you are happy with the sample I posted, you just need to change the page code to get the Contratto entity by using ContrattoManager, then create the appropriate page controls and link them in the OnLoad code as I have done. Try that first and ask if you get into problems with it.
If you are wanting to show a whole list of Contratto objects on the page, then you really don't want to be doing it manually and should seriously consider using proper data controls such as a ListView
Phil
|
|

April 3rd, 2009, 10:12 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can you tell me if this is OK?
Contratto contratto = new Contratto();
contratto = ContrattoManager.GetItem(id);
now in contratto.*** I've all items of the selected record
it's OK??
Thanks
|
|

April 3rd, 2009, 10:47 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Yes that will only get one Contratto instance and so you can manually bind it if you wish.
But pleeeease don't do what so many people do and create an object instance then immediately overwrite it with another one. Just use either of these:
Code:
Contratto contratto = ContrattoManager.GetItem(id);
/* OR */
Contratto contratto;
contratto = ContrattoManager.GetItem(id);
Phil
|
|
 |