databinding to a property of a class ?

  • Thread starter Thread starter Colin Robinson
  • Start date Start date
C

Colin Robinson

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,


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
 
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;
 
Back
Top