473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Binding custom object to formview

Hi,

I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for example
a custom type Address which contains properties such as StreetAddress, City,
County, Country etc how do i bind to those properties on the same form view?

I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?

Thanks for your help

Ash
Feb 19 '07 #1
8 11946

Me.txtOne.DataBindings.Add(New Binding("Text", Me.m_user,
"Address.StreetAddress"))
Me.txtTwo.DataBindings.Add(New Binding("Text", Me.m_user,
"Address.StreetAddress"))

try this.

it works in winforms. not sure about asp.net off hand.


"Ash" <As*@discussions.microsoft.comwrote in message
news:3D**********************************@microsof t.com...
Hi,

I have an object, for example User. User contains various properties which
i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for
example
a custom type Address which contains properties such as StreetAddress,
City,
County, Country etc how do i bind to those properties on the same form
view?
>
I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?

Thanks for your help

Ash

Feb 19 '07 #2
Hi Ash,

Bind() method does not support complex binding expressions (i.e. set a
property of object-type property as you presented), but Eval() does.
Unfortunatelly, Eval() can only be used to evaluate expression when data is
bound (one way), and not to read changed value back. So you if go for Eval,
you will end up handling FormView's events to set insertparameters or
updateparanmeters manually.

--
Milosz
"Ash" wrote:
Hi,

I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for example
a custom type Address which contains properties such as StreetAddress, City,
County, Country etc how do i bind to those properties on the same form view?

I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?

Thanks for your help

Ash
Feb 19 '07 #3

If it works , please post a response.

I'd like to know myself.



"sloan" <sl***@ipass.netwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>
Me.txtOne.DataBindings.Add(New Binding("Text", Me.m_user,
"Address.StreetAddress"))
Me.txtTwo.DataBindings.Add(New Binding("Text", Me.m_user,
"Address.StreetAddress"))

try this.

it works in winforms. not sure about asp.net off hand.


"Ash" <As*@discussions.microsoft.comwrote in message
news:3D**********************************@microsof t.com...
Hi,

I have an object, for example User. User contains various properties
which
i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for
example
a custom type Address which contains properties such as StreetAddress,
City,
County, Country etc how do i bind to those properties on the same form
view?

I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?

Thanks for your help

Ash


Feb 19 '07 #4
Hi thanks for the information,

What about a GridView i have it displaying objects but if one of the
properties is an object itself of which i need a property of how would i
access that? for example below i BillingType is an object but I need to
access a property of that object?

<asp:BoundField DataField="BillingType" HeaderText="Billing Type"
SortExpression="BillingType" />

Thanks for your help

"Milosz Skalecki [MCAD]" wrote:
Hi Ash,

Bind() method does not support complex binding expressions (i.e. set a
property of object-type property as you presented), but Eval() does.
Unfortunatelly, Eval() can only be used to evaluate expression when data is
bound (one way), and not to read changed value back. So you if go for Eval,
you will end up handling FormView's events to set insertparameters or
updateparanmeters manually.

--
Milosz
"Ash" wrote:
Hi,

I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for example
a custom type Address which contains properties such as StreetAddress, City,
County, Country etc how do i bind to those properties on the same form view?

I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?

Thanks for your help

Ash
Feb 20 '07 #5
Hi there,

I think complex data binding also does not work for BoundField, you have to
use templatefield with Eval("Property.AnotherProperty")
--
Milosz
"Ash" wrote:
Hi thanks for the information,

What about a GridView i have it displaying objects but if one of the
properties is an object itself of which i need a property of how would i
access that? for example below i BillingType is an object but I need to
access a property of that object?

<asp:BoundField DataField="BillingType" HeaderText="Billing Type"
SortExpression="BillingType" />

Thanks for your help

"Milosz Skalecki [MCAD]" wrote:
Hi Ash,

Bind() method does not support complex binding expressions (i.e. set a
property of object-type property as you presented), but Eval() does.
Unfortunatelly, Eval() can only be used to evaluate expression when data is
bound (one way), and not to read changed value back. So you if go for Eval,
you will end up handling FormView's events to set insertparameters or
updateparanmeters manually.

--
Milosz
"Ash" wrote:
Hi,
>
I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for example
a custom type Address which contains properties such as StreetAddress, City,
County, Country etc how do i bind to those properties on the same form view?
>
I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?
>
Thanks for your help
>
Ash
Feb 20 '07 #6
Thanks for your responses they have been great help.

i have got everything to work so far but i am a bit confused as to where i
should put the event handeling you were discussing earlier about the update
and insert parameters when using Eval?

"So you if go for Eval, you will end up handling FormView's events to set
insertparameters or updateparanmeters manually.
"
How do i make sure that my code only gets executed not the formviews default
handeling?

Thanks
"Milosz Skalecki [MCAD]" wrote:
Hi there,

I think complex data binding also does not work for BoundField, you have to
use templatefield with Eval("Property.AnotherProperty")
--
Milosz
"Ash" wrote:
Hi thanks for the information,

What about a GridView i have it displaying objects but if one of the
properties is an object itself of which i need a property of how would i
access that? for example below i BillingType is an object but I need to
access a property of that object?

<asp:BoundField DataField="BillingType" HeaderText="Billing Type"
SortExpression="BillingType" />

Thanks for your help

"Milosz Skalecki [MCAD]" wrote:
Hi Ash,
>
Bind() method does not support complex binding expressions (i.e. set a
property of object-type property as you presented), but Eval() does.
Unfortunatelly, Eval() can only be used to evaluate expression when data is
bound (one way), and not to read changed value back. So you if go for Eval,
you will end up handling FormView's events to set insertparameters or
updateparanmeters manually.
>
--
Milosz
>
>
"Ash" wrote:
>
Hi,

I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for example
a custom type Address which contains properties such as StreetAddress, City,
County, Country etc how do i bind to those properties on the same form view?

I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?

Thanks for your help

Ash
Feb 20 '07 #7
Howdy,

Sorry, i meant handling objectdatasource events, of course if you use
formview in conjunction with any datasource control, if not i can't really
see the posint of using FormView for edit/insert operations. I prepared a
simple example:

-- begin aspx code --
<script runat="server">

private void SetPersonInaccessibleDetails(Person person)
{
if (person == null)
throw new NullReferenceException("person");

TextBox textBox = (TextBox)fv.FindControl("FirstNameTextBox");
person.Name.FirstName = textBox.Text;

textBox = (TextBox)fv.FindControl("LastNameTextBox");
person.Name.LastName = textBox.Text;
}
protected void ds_Inserting(object sender, ObjectDataSourceMethodEventArgs
e)
{
SetPersonInaccessibleDetails((Person)e.InputParame ters[0]);
}

protected void ds_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
SetPersonInaccessibleDetails((Person)e.InputParame ters[0]);
}
</script>

<asp:ObjectDataSource runat="server" ID="ds"
TypeName="PersonManager"
SelectMethod="Get" DataObjectTypeName="Person"
UpdateMethod="Update" InsertMethod="Insert"
OnInserting="ds_Inserting" OnUpdating="ds_Updating">
<SelectParameters>
<asp:Parameter Name="id" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

<asp:FormView runat="server" ID="fv"
DefaultMode="Insert"
DataSourceID="ds"
DataKeyNames="Id">
<InsertItemTemplate>
Age:
<asp:TextBox ID="AgeTextBox" runat="server"
Text='<%# Bind("Age") %>'>
</asp:TextBox><br />
First Name:
<asp:TextBox ID="FirstNameTextBox" runat="server">
</asp:TextBox><br />
Last Name:
<asp:TextBox ID="LastNameTextBox" runat="server">
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<EditItemTemplate>
Age:
<asp:TextBox ID="AgeTextBox" runat="server"
Text='<%# Bind("Age") %>'>
</asp:TextBox><br />
First Name:
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%# Eval("Name.FirstName") %>'>
</asp:TextBox><br />
Last Name:
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%# Eval("Name.LastName") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update" Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
Id:
<asp:Label ID="IdLabel" runat="server"
Text='<%# Eval("Id") %>'></asp:Label><br />
Age:
<asp:Label ID="AgeLabel" runat="server"
Text='<%# Eval("Age") %>'></asp:Label><br />
Name:
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name.FirstName") %>'/><br />
<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False" CommandName="New"
Text="New">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>

-- end aspx code --

-- begin c# code (separate file with pesudo: D business object --
-- end c# code --

/// <summary>
/// Summary description for ProductsBLL
/// </summary>
public class PersonManager
{

public void Update(Person person)
{
}

public void Insert(Person person)
{
//
}

private Random random = new Random();
public Person Get(int id)
{
string str = id.ToString();

Person person = new Person();

person.Id = id;
person.Age = this.random.Next(1, 100);
person.Name.FirstName = "FirstName" + str;
person.Name.LastName = "LastName" + str;

return person;
}
}
[Serializable]
public class Person
{

private int id;
/// <summary>
///
/// </summary>
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
private FullName fullName = new FullName();
public FullName Name
{
get
{
return this.fullName;
}
}

private int age = 18;
/// <summary>
///
/// </summary>
public int Age
{
get
{
return this.age;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.age = value;
}
}

}

public class FullName
{
public FullName()
{
}

private string firstName;
/// <summary>
///
/// </summary>
public string FirstName
{
get
{
return this.firstName == null ?
String.Empty : this.lastName;
}
set
{
this.firstName = value;
}
}

private string lastName;
/// <summary>
///
/// </summary>
public string LastName
{
get
{
return this.lastName == null ?
String.Empty : this.lastName;
}
set
{
this.lastName = value;
}
}
}
--
Milosz
"Ash" wrote:
Thanks for your responses they have been great help.

i have got everything to work so far but i am a bit confused as to where i
should put the event handeling you were discussing earlier about the update
and insert parameters when using Eval?

"So you if go for Eval, you will end up handling FormView's events to set
insertparameters or updateparanmeters manually.
"
How do i make sure that my code only gets executed not the formviews default
handeling?

Thanks
"Milosz Skalecki [MCAD]" wrote:
Hi there,

I think complex data binding also does not work for BoundField, you have to
use templatefield with Eval("Property.AnotherProperty")
--
Milosz
"Ash" wrote:
Hi thanks for the information,
>
What about a GridView i have it displaying objects but if one of the
properties is an object itself of which i need a property of how would i
access that? for example below i BillingType is an object but I need to
access a property of that object?
>
<asp:BoundField DataField="BillingType" HeaderText="Billing Type"
SortExpression="BillingType" />
>
Thanks for your help
>
>
>
"Milosz Skalecki [MCAD]" wrote:
>
Hi Ash,

Bind() method does not support complex binding expressions (i.e. set a
property of object-type property as you presented), but Eval() does.
Unfortunatelly, Eval() can only be used to evaluate expression when data is
bound (one way), and not to read changed value back. So you if go for Eval,
you will end up handling FormView's events to set insertparameters or
updateparanmeters manually.

--
Milosz


"Ash" wrote:

Hi,
>
I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property which is not a string, for example
a custom type Address which contains properties such as StreetAddress, City,
County, Country etc how do i bind to those properties on the same form view?
>
I have tried doing Bind("Address.StreetAddress") but this just raises a
compile error?
>
Thanks for your help
>
Ash
Feb 20 '07 #8
Thanks works great!

"Milosz Skalecki [MCAD]" wrote:
Howdy,

Sorry, i meant handling objectdatasource events, of course if you use
formview in conjunction with any datasource control, if not i can't really
see the posint of using FormView for edit/insert operations. I prepared a
simple example:

-- begin aspx code --
<script runat="server">

private void SetPersonInaccessibleDetails(Person person)
{
if (person == null)
throw new NullReferenceException("person");

TextBox textBox = (TextBox)fv.FindControl("FirstNameTextBox");
person.Name.FirstName = textBox.Text;

textBox = (TextBox)fv.FindControl("LastNameTextBox");
person.Name.LastName = textBox.Text;
}
protected void ds_Inserting(object sender, ObjectDataSourceMethodEventArgs
e)
{
SetPersonInaccessibleDetails((Person)e.InputParame ters[0]);
}

protected void ds_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
SetPersonInaccessibleDetails((Person)e.InputParame ters[0]);
}
</script>

<asp:ObjectDataSource runat="server" ID="ds"
TypeName="PersonManager"
SelectMethod="Get" DataObjectTypeName="Person"
UpdateMethod="Update" InsertMethod="Insert"
OnInserting="ds_Inserting" OnUpdating="ds_Updating">
<SelectParameters>
<asp:Parameter Name="id" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

<asp:FormView runat="server" ID="fv"
DefaultMode="Insert"
DataSourceID="ds"
DataKeyNames="Id">
<InsertItemTemplate>
Age:
<asp:TextBox ID="AgeTextBox" runat="server"
Text='<%# Bind("Age") %>'>
</asp:TextBox><br />
First Name:
<asp:TextBox ID="FirstNameTextBox" runat="server">
</asp:TextBox><br />
Last Name:
<asp:TextBox ID="LastNameTextBox" runat="server">
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<EditItemTemplate>
Age:
<asp:TextBox ID="AgeTextBox" runat="server"
Text='<%# Bind("Age") %>'>
</asp:TextBox><br />
First Name:
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%# Eval("Name.FirstName") %>'>
</asp:TextBox><br />
Last Name:
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%# Eval("Name.LastName") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update" Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
Id:
<asp:Label ID="IdLabel" runat="server"
Text='<%# Eval("Id") %>'></asp:Label><br />
Age:
<asp:Label ID="AgeLabel" runat="server"
Text='<%# Eval("Age") %>'></asp:Label><br />
Name:
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name.FirstName") %>'/><br />
<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False" CommandName="New"
Text="New">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>

-- end aspx code --

-- begin c# code (separate file with pesudo: D business object --
-- end c# code --

/// <summary>
/// Summary description for ProductsBLL
/// </summary>
public class PersonManager
{

public void Update(Person person)
{
}

public void Insert(Person person)
{
//
}

private Random random = new Random();
public Person Get(int id)
{
string str = id.ToString();

Person person = new Person();

person.Id = id;
person.Age = this.random.Next(1, 100);
person.Name.FirstName = "FirstName" + str;
person.Name.LastName = "LastName" + str;

return person;
}
}
[Serializable]
public class Person
{

private int id;
/// <summary>
///
/// </summary>
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
private FullName fullName = new FullName();
public FullName Name
{
get
{
return this.fullName;
}
}

private int age = 18;
/// <summary>
///
/// </summary>
public int Age
{
get
{
return this.age;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.age = value;
}
}

}

public class FullName
{
public FullName()
{
}

private string firstName;
/// <summary>
///
/// </summary>
public string FirstName
{
get
{
return this.firstName == null ?
String.Empty : this.lastName;
}
set
{
this.firstName = value;
}
}

private string lastName;
/// <summary>
///
/// </summary>
public string LastName
{
get
{
return this.lastName == null ?
String.Empty : this.lastName;
}
set
{
this.lastName = value;
}
}
}
--
Milosz
"Ash" wrote:
Thanks for your responses they have been great help.

i have got everything to work so far but i am a bit confused as to where i
should put the event handeling you were discussing earlier about the update
and insert parameters when using Eval?

"So you if go for Eval, you will end up handling FormView's events to set
insertparameters or updateparanmeters manually.
"
How do i make sure that my code only gets executed not the formviews default
handeling?

Thanks
"Milosz Skalecki [MCAD]" wrote:
Hi there,
>
I think complex data binding also does not work for BoundField, you have to
use templatefield with Eval("Property.AnotherProperty")
--
Milosz
>
>
"Ash" wrote:
>
Hi thanks for the information,

What about a GridView i have it displaying objects but if one of the
properties is an object itself of which i need a property of how would i
access that? for example below i BillingType is an object but I need to
access a property of that object?

<asp:BoundField DataField="BillingType" HeaderText="Billing Type"
SortExpression="BillingType" />

Thanks for your help



"Milosz Skalecki [MCAD]" wrote:

Hi Ash,
>
Bind() method does not support complex binding expressions (i.e. set a
property of object-type property as you presented), but Eval() does.
Unfortunatelly, Eval() can only be used to evaluate expression when data is
bound (one way), and not to read changed value back. So you if go for Eval,
you will end up handling FormView's events to set insertparameters or
updateparanmeters manually.
>
--
Milosz
>
>
"Ash" wrote:
>
Hi,
Feb 20 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Frank Wisniewski | last post by:
I have the following persudo code: //My Form Class class Form1 { //Local Variable for my custom object private MyCustomObject1 //Constructor for Form Class public Constructor{
1
by: Thomas Jespersen | last post by:
Hello How do I databind a ASP.NET Texbox to a custom object. I a WinForm I do it like this: Dim myCustomerObject As Customer() Private Sub CreateNewCustomer myCustomerObject = New...
1
by: Weston Weems | last post by:
I've got a collection of objects say fruit. and fruit has a fairly decent heirachial structure not just flat props. I've got my repeater to bind to my fruit collection just fine, but what I want...
0
by: Tommaso Caldarola | last post by:
2.0 I have Class1 and Class2. Class1 has a Class2 property (parent object). Now in my Form I have a ComboBox to manage all the items of collection<Class2>. How to bind correctly Class1 property...
3
by: cannontrodder | last post by:
I am displaying names and other details of my users in a Formview control by binding my custom business object to it. My custom object also has a property that is a collection of boolean values and...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
2
by: Greg | last post by:
Hello, I am trying to bind a GridView to a custom object I have created. First, here is what I'm trying to do: I have a wizard for adding/editing Users. When the wizard begins, a User...
1
by: timnels | last post by:
I have a custom business object that implements INotifyPropertyChanged and IEditableObject. The collection of these objects inherits from BindingList<T>. I created a BindingSource from which a...
0
by: LieWait | last post by:
I have a custom business object that implements INotifyPropertyChanged and IEditableObject. The collection of these objects inherits from BindingList<T>. I created a BindingSource from which a grid...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.