databinding to a property of a class ?

Discussion in 'ASP .Net Web Controls' started by Colin Robinson, Aug 9, 2004.

  1. Help please

    I have an example class called Person with 2 public properties Firstname
    and Lastname, I cant create a textbox on an asp.net form bound to the
    Person.Firstname property

    Can anyone help with the required syntax?

    if i do TExtbox1.text = x.firstname it works ok, but I want to edit the
    textbox setting the value of x.firstname.

    I am after a webcontrol that will edit person class with a button that will
    save person class back to the database.

    Here is the page code so far :

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init

    'CODEGEN: This method call is required by the Web Form Designer

    'Do not modify it using the code editor.

    InitializeComponent()

    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    Dim x As New person

    x.firstname = "fred"

    Page.DataBind()

    'Textbox2 has a databinding on the text property = Person.firstname



    End Sub

    End Class

    Public Class person

    Private _firstname

    Public Property firstname() As String

    Get

    Return _firstname

    End Get

    Set(ByVal Value As String)

    _firstname = Value

    End Set

    End Property

    End Class
     
    Colin Robinson, Aug 9, 2004
    #1
    1. Advertising

  2. Bharat,


    I'm assuming from your explanation that the person instance is out of scope
    when page load is complete.

    When it becomes a web user control embedded on a page where should oPerson
    be declared so that its properties can be set from the control but
    accessible from the containing page. I am thinking I want to save oPerson
    into session State so that its properties can become available to other
    pages in the browser session?

    thanks for the clear explanation so far


    Colin


    "Bharat Biyani" <> wrote in message
    news:...
    > Hi Colin,
    >
    > Follow these steps:
    > 1) Declare a instance of the Person object in your class. YOu are

    currently
    > doing it in the method. the instance should be a class member.
    >
    > 2)In aspx file make changes as shown below:
    > <asp:TextBox id="TextBox1" runat="server" Text=<%#p.FName%>/>
    > Here p is the instance name of the Person class declared in the code

    behind
    > file.
    >
    > 3) In the PageLoad event binf the textbox to the object y calling the
    > TextBox1.DataBind() method.
    >
    > This will bind the textbox to the object.
    >
    > ---
    > Bharat Biyani ()
    > http://www.orcim.com
    > "Colin Robinson" wrote:
    >
    > > Help please
    > >
    > > I have an example class called Person with 2 public properties

    Firstname
    > > and Lastname, I cant create a textbox on an asp.net form bound to the
    > > Person.Firstname property
    > >
    > > Can anyone help with the required syntax?
    > >
    > > if i do TExtbox1.text = x.firstname it works ok, but I want to edit the
    > > textbox setting the value of x.firstname.
    > >
    > > I am after a webcontrol that will edit person class with a button that

    will
    > > save person class back to the database.
    > >
    > > Here is the page code so far :
    > >
    > > #Region " Web Form Designer Generated Code "
    > >
    > > 'This call is required by the Web Form Designer.
    > >
    > > <System.Diagnostics.DebuggerStepThrough()> Private Sub

    InitializeComponent()
    > >
    > > End Sub
    > >
    > > Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    > >
    > > 'NOTE: The following placeholder declaration is required by the Web Form
    > > Designer.
    > >
    > > 'Do not delete or move it.
    > >
    > > Private designerPlaceholderDeclaration As System.Object
    > >
    > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Init
    > >
    > > 'CODEGEN: This method call is required by the Web Form Designer
    > >
    > > 'Do not modify it using the code editor.
    > >
    > > InitializeComponent()
    > >
    > > End Sub
    > >
    > > #End Region
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > 'Put user code to initialize the page here
    > >
    > > Dim x As New person
    > >
    > > x.firstname = "fred"
    > >
    > > Page.DataBind()
    > >
    > > 'Textbox2 has a databinding on the text property = Person.firstname
    > >
    > >
    > >
    > > End Sub
    > >
    > > End Class
    > >
    > > Public Class person
    > >
    > > Private _firstname
    > >
    > > Public Property firstname() As String
    > >
    > > Get
    > >
    > > Return _firstname
    > >
    > > End Get
    > >
    > > Set(ByVal Value As String)
    > >
    > > _firstname = Value
    > >
    > > End Set
    > >
    > > End Property
    > >
    > > End Class
    > >
    > >
    > >
    > >
    > >
     
    Colin Robinson, Aug 11, 2004
    #2
    1. Advertising

  3. Hi,

    You can save the Person object in the session. However while creating an
    instance of that object it should be done at class level so that it is
    available for databinding.
    You could do something like this:

    Person oPer=(Person)Session[objPerson]; //declare as a class variable.
    ....
    Do databinding
    ....
    //Save changes to person as per the values in the textbox.
    Save the person object back to the session.
    Session[objPerson]=oPer;

    ---
    Bharat Biyani ()
    http://www.orcim.com
    "Colin Robinson" wrote:

    > Bharat,
    >
    >
    > I'm assuming from your explanation that the person instance is out of scope
    > when page load is complete.
    >
    > When it becomes a web user control embedded on a page where should oPerson
    > be declared so that its properties can be set from the control but
    > accessible from the containing page. I am thinking I want to save oPerson
    > into session State so that its properties can become available to other
    > pages in the browser session?
    >
    > thanks for the clear explanation so far
    >
    >
    > Colin
    >
    >
    > "Bharat Biyani" <> wrote in message
    > news:...
    > > Hi Colin,
    > >
    > > Follow these steps:
    > > 1) Declare a instance of the Person object in your class. YOu are

    > currently
    > > doing it in the method. the instance should be a class member.
    > >
    > > 2)In aspx file make changes as shown below:
    > > <asp:TextBox id="TextBox1" runat="server" Text=<%#p.FName%>/>
    > > Here p is the instance name of the Person class declared in the code

    > behind
    > > file.
    > >
    > > 3) In the PageLoad event binf the textbox to the object y calling the
    > > TextBox1.DataBind() method.
    > >
    > > This will bind the textbox to the object.
    > >
    > > ---
    > > Bharat Biyani ()
    > > http://www.orcim.com
    > > "Colin Robinson" wrote:
    > >
    > > > Help please
    > > >
    > > > I have an example class called Person with 2 public properties

    > Firstname
    > > > and Lastname, I cant create a textbox on an asp.net form bound to the
    > > > Person.Firstname property
    > > >
    > > > Can anyone help with the required syntax?
    > > >
    > > > if i do TExtbox1.text = x.firstname it works ok, but I want to edit the
    > > > textbox setting the value of x.firstname.
    > > >
    > > > I am after a webcontrol that will edit person class with a button that

    > will
    > > > save person class back to the database.
    > > >
    > > > Here is the page code so far :
    > > >
    > > > #Region " Web Form Designer Generated Code "
    > > >
    > > > 'This call is required by the Web Form Designer.
    > > >
    > > > <System.Diagnostics.DebuggerStepThrough()> Private Sub

    > InitializeComponent()
    > > >
    > > > End Sub
    > > >
    > > > Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    > > >
    > > > 'NOTE: The following placeholder declaration is required by the Web Form
    > > > Designer.
    > > >
    > > > 'Do not delete or move it.
    > > >
    > > > Private designerPlaceholderDeclaration As System.Object
    > > >
    > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Init
    > > >
    > > > 'CODEGEN: This method call is required by the Web Form Designer
    > > >
    > > > 'Do not modify it using the code editor.
    > > >
    > > > InitializeComponent()
    > > >
    > > > End Sub
    > > >
    > > > #End Region
    > > >
    > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Load
    > > >
    > > > 'Put user code to initialize the page here
    > > >
    > > > Dim x As New person
    > > >
    > > > x.firstname = "fred"
    > > >
    > > > Page.DataBind()
    > > >
    > > > 'Textbox2 has a databinding on the text property = Person.firstname
    > > >
    > > >
    > > >
    > > > End Sub
    > > >
    > > > End Class
    > > >
    > > > Public Class person
    > > >
    > > > Private _firstname
    > > >
    > > > Public Property firstname() As String
    > > >
    > > > Get
    > > >
    > > > Return _firstname
    > > >
    > > > End Get
    > > >
    > > > Set(ByVal Value As String)
    > > >
    > > > _firstname = Value
    > > >
    > > > End Set
    > > >
    > > > End Property
    > > >
    > > > End Class
    > > >
    > > >
    > > >
    > > >
    > > >

    >
    >
    >
     
    Bharat Biyani, Aug 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georg Mayer
    Replies:
    2
    Views:
    11,344
    Georg Mayer
    Jun 22, 2004
  2. Colin Robinson

    databinding to a property of a class ?

    Colin Robinson, Aug 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,408
    =?Utf-8?B?QmhhcmF0IEJpeWFuaQ==?=
    Aug 12, 2004
  3. E11
    Replies:
    1
    Views:
    4,792
    Thomas Weidenfeller
    Oct 12, 2005
  4. jobs
    Replies:
    0
    Views:
    1,632
  5. Earl Teigrob
    Replies:
    1
    Views:
    408
    Shravan
    Mar 1, 2004
Loading...

Share This Page