Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > databinding to a property of a class ?

Reply
Thread Tools

databinding to a property of a class ?

 
 
Colin Robinson
Guest
Posts: n/a
 
      08-09-2004
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




 
Reply With Quote
 
 
 
 
Colin Robinson
Guest
Posts: n/a
 
      08-11-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ((E-Mail Removed))
> 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
> >
> >
> >
> >
> >



 
Reply With Quote
 
 
 
 
Bharat Biyani
Guest
Posts: n/a
 
      08-12-2004
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 ((E-Mail Removed))
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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 ((E-Mail Removed))
> > 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
> > >
> > >
> > >
> > >
> > >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databinding expressions are only supported on objects that have a DataBinding event jobs ASP .Net 0 09-25-2007 11:54 PM
Gridview encoding, or how to run commands before gridview's default databinding, or, how do I disable default databinding at all? Sergei Shelukhin ASP .Net 1 11-12-2006 01:43 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
databinding to a property of a class ? Colin Robinson ASP .Net 2 08-12-2004 05:59 AM
DataGrid Custom Column Error when DataBinding "does not contain a definition for 'DataBinding'" Earl Teigrob ASP .Net Datagrid Control 1 03-01-2004 04:52 AM



Advertisments