By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,266 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,266 IT Pros & Developers. It's quick & easy.

databinding to a property of a class ?

P: n/a
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


Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <Bh**********@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
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 (bs*@orcim.com)
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


Nov 18 '05 #2

P: n/a
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 (bs*@orcim.com)
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" <Bh**********@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
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 (bs*@orcim.com)
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



Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.