Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old March 29th, 2009, 05:08 PM
Authorized User
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
  #2 (permalink)  
Old March 29th, 2009, 06:04 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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
  #3 (permalink)  
Old March 30th, 2009, 05:26 PM
Authorized User
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
  #4 (permalink)  
Old March 31st, 2009, 05:47 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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..
  #5 (permalink)  
Old April 2nd, 2009, 10:38 AM
Authorized User
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #6 (permalink)  
Old April 2nd, 2009, 02:50 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #7 (permalink)  
Old April 2nd, 2009, 06:37 PM
Authorized User
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #8 (permalink)  
Old April 2nd, 2009, 07:02 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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
  #9 (permalink)  
Old April 3rd, 2009, 10:12 AM
Authorized User
 
Join Date: Mar 2009
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #10 (permalink)  
Old April 3rd, 2009, 10:47 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill out a web form mendy VB How-To 19 March 31st, 2015 02:28 PM
Using VBScript to Fill Out a Web Form adley VBScript 11 December 8th, 2011 01:47 PM
Fill datasheet/Continuous form with vba code only Galateo Access VBA 0 May 9th, 2008 09:39 AM
auto-fill form crmpicco HTML Code Clinic 2 April 21st, 2005 06:42 AM
dealing with CLASS IN NTIER drachx General .NET 1 October 12th, 2004 11:26 PM





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