473,698 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bind an Array of Custom Objects

Jed
I have a web service method that returns an array of custom objects marked
serializeable with fully described public properties.

When I bind the results to a DataGrid I can access the properties in the
ItemDataBound event of the codebehind but I can't access them declaratively
in the HTML code?

Here is the code to call the method.
net.mysite.www.WSInterface proxy;
proxy = new net.mysite.www.WSInterface();
ProjectList[] pl = proxy.getProjec ts("%",false,tr ue);
this.DataGrid1. DataSource = pl;
this.DataGrid1. DataBind();

Here is the code to access the property in the Item databound event.
lbl.Text = ((ProjectList)d g.DataItem).Cli entName;

Here is the code that won't work on the HTML side:
Option 1:
<asp:BoundColum n HeaderText="Cli ent Contact"
DataField="Clie ntContact"></asp:BoundColumn >

Option 2:
<asp:TemplateCo lumn HeaderText="Con tact">
<ItemTemplate>< %# DataBinder.Eval (Container.Data Item,
"ClientContact" )%></ItemTemplate>
</asp:TemplateCol umn>

Here is the error:
DataBinder.Eval : 'net.mysite.www .ProjectList' does not contain a property
with the name ClientContact.

Why can I access the property in the ItemDataBound event but not on the
declarative side?

The Page obviously know what kind of Object the data item is, otherwise the
error could not report it.

Any ideas? Thanks!
Nov 18 '05 #1
7 2559
Jed, dumb question: in your one that works, you use "ClientName ", but in the
ones that don't you use "ClientContact" . Is that right?

Bill

"Jed" wrote:
I have a web service method that returns an array of custom objects marked
serializeable with fully described public properties.

When I bind the results to a DataGrid I can access the properties in the
ItemDataBound event of the codebehind but I can't access them declaratively
in the HTML code?

Here is the code to call the method.
net.mysite.www.WSInterface proxy;
proxy = new net.mysite.www.WSInterface();
ProjectList[] pl = proxy.getProjec ts("%",false,tr ue);
this.DataGrid1. DataSource = pl;
this.DataGrid1. DataBind();

Here is the code to access the property in the Item databound event.
lbl.Text = ((ProjectList)d g.DataItem).Cli entName;

Here is the code that won't work on the HTML side:
Option 1:
<asp:BoundColum n HeaderText="Cli ent Contact"
DataField="Clie ntContact"></asp:BoundColumn >

Option 2:
<asp:TemplateCo lumn HeaderText="Con tact">
<ItemTemplate>< %# DataBinder.Eval (Container.Data Item,
"ClientContact" )%></ItemTemplate>
</asp:TemplateCol umn>

Here is the error:
DataBinder.Eval : 'net.mysite.www .ProjectList' does not contain a property
with the name ClientContact.

Why can I access the property in the ItemDataBound event but not on the
declarative side?

The Page obviously know what kind of Object the data item is, otherwise the
error could not report it.

Any ideas? Thanks!

Nov 18 '05 #2
Jed
Unfortunately, no.

It is a good catch though. I noticed that after I posted. Both ClientName
and ClientContact are legitimate string properties of the object. I get the
same behaviour with both.

Thanks!

"Bill Borg" wrote:
Jed, dumb question: in your one that works, you use "ClientName ", but in the
ones that don't you use "ClientContact" . Is that right?

Bill

Nov 18 '05 #3
Hello Jed,

Because objects serialized from web services do not contain public properties,
they contain public fields. Fields are not able to be used for declarative
data binding.

The reason it works in your code-behind, is because you are referencing a
field, which is valid in code.

--
Matt Berther
http://www.mattberther.com
I have a web service method that returns an array of custom objects
marked serializeable with fully described public properties.

When I bind the results to a DataGrid I can access the properties in
the ItemDataBound event of the codebehind but I can't access them
declaratively in the HTML code?

Here is the code to call the method.
net.mysite.www.WSInterface proxy;
proxy = new net.mysite.www.WSInterface();
ProjectList[] pl = proxy.getProjec ts("%",false,tr ue);
this.DataGrid1. DataSource = pl;
this.DataGrid1. DataBind();
Here is the code to access the property in the Item databound event.
lbl.Text = ((ProjectList)d g.DataItem).Cli entName;

Here is the code that won't work on the HTML side:
Option 1:
<asp:BoundColum n HeaderText="Cli ent Contact"
DataField="Clie ntContact"></asp:BoundColumn >
Option 2:
<asp:TemplateCo lumn HeaderText="Con tact">
<ItemTemplate>< %# DataBinder.Eval (Container.Data Item,
"ClientContact" )%></ItemTemplate>
</asp:TemplateCol umn>
Here is the error:
DataBinder.Eval : 'net.mysite.www .ProjectList' does not contain a
property
with the name ClientContact.
Why can I access the property in the ItemDataBound event but not on
the declarative side?

The Page obviously know what kind of Object the data item is,
otherwise the error could not report it.

Any ideas? Thanks!

Nov 18 '05 #4
Jed
Thanks, Matt,

Huh. I will look into that. I didn't know that there was even a "Field"
concept for class objects, nor do I even now understand how the Framework
maps my Property to this virtual "Field" value/construct.

Is there a best practices on how to overcome this (i.e.,custom
deserializer)? or is just impossible when working with web services?

Thanks again,
Jeff

"Matt Berther" wrote:
Hello Jed,

Because objects serialized from web services do not contain public properties,
they contain public fields. Fields are not able to be used for declarative
data binding.

The reason it works in your code-behind, is because you are referencing a
field, which is valid in code.

--
Matt Berther
http://www.mattberther.com

Nov 18 '05 #5
Hello Jed,

The differences between fields and properties are:

class Foo
{
public string Foo; // this is a field

private string bar;
public string Bar // this is a property
{
get { return bar; }
set { bar = value; }
}
}

According to this article [1] allowing databinding to fields is not something
thats going to change...

[1]http://www.nikhilk.net/Entry.aspx?id=4 2

Now, from what I've seen of ASP.NET 2.0, the class generated from the WSDL
has get/set properties instead of fields, so this might be good news for you.

Other than that, I imagine you could probably avoid using the web reference
and craft your communication layer yourself and get around this...

--
Matt Berther
http://www.mattberther.com
Thanks, Matt,

Huh. I will look into that. I didn't know that there was even a
"Field" concept for class objects, nor do I even now understand how
the Framework maps my Property to this virtual "Field"
value/construct.

Is there a best practices on how to overcome this (i.e.,custom
deserializer)? or is just impossible when working with web services?

Thanks again,
Jeff
"Matt Berther" wrote:
Hello Jed,

Because objects serialized from web services do not contain public
properties, they contain public fields. Fields are not able to be
used for declarative data binding.

The reason it works in your code-behind, is because you are
referencing a field, which is valid in code.

--
Matt Berther
http://www.mattberther.com

Nov 18 '05 #6
Hi,

I was searching the internet to find out how to bind the selected
columns of an array of custom objects to the datagrid and i found this
post on google groups. I want you to please have a look at my sample
code and let me know if i am on the right track or not. I am new to
..net and would like to have some guidance from u guys. here is what
iam trying to do:

1) get an array of objects from the web service method(for ex: lets
say there are 4 fields/properties for each object)
2) i want to bind only 2 fields/properties to the datagrid.
3) i have written an event handler that assigns the value of the
property to the literal control inside a datagrid's item template
column.
4) attached the event handler to the datagrid by using the
OnItemDataBound event of the datagrid
here is the code that i have:

event handler for the OnItemDataBound event :

public void Item_Bound(obje ct sender, DataGridItemEve ntArgs e)
{
if(e.Item.ItemT ype == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
Literal l = (Literal)e.Item .FindControl("l it");
if(l.Text == String.Empty)
{
l.Text = ((FormsOrdering WS.Order)
e.Item.DataItem ).OrderDate.ToS tring();
}
}
}

HTML code for datagrid:

<asp:DataGrid OnItemDataBound ="Item_Bound".. ....>
<asp:Columns>
<asp:TemplateCo lumn HeaderText="Ord er Date">
<ItemTemplate >
<asp:Literal ID="lit" Runat="server"> </asp:Literal>
</ItemTemplate>
</asp:TemplateCol umn>
</asp:Columns>
</asp:DataGrid>
please suggest changes if iam wrong. Thank you
Nov 18 '05 #7
Jed
I don't see anything wrong with your approach.

In the end, I created a method to convert my object array to a DataSet so
that I could bind to the DataSet in the code, and also declaratively access
the property information from the HTML.

The sample is binding to a DropDown, which is the same as binding to a Grid
except that you have to specify, in the code, which fields represent the
Value and Text for each item. If you were using a DataGrid you could simple
use a BoundColumn to display the property values.

Here is the code I use:
/// <summary>
/// A method to convert an array of objects returned from a web service into
/// a DataSet so that they can be bound declaratively in the HTML. This is
to work
/// around the fact that the default Visual Studio WebServices references
/// deserialize object properties as fields instead of true get/set
properties.
/// </summary>
/// <param name="objectArr ay">The array of objects.</param>
/// <returns>A DataSet with a table named after the objectType.</returns>
public static DataSet ConvertObjectAr rayToDataSet(ob ject objectArray)
{
StringWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(o bjectArray.GetT ype());
serializer.Seri alize(writer, objectArray);
DataSet ds = new DataSet();
StringReader reader = new StringReader(wr iter.ToString() );
ds.ReadXml(read er);
return ds;
}

// Here is an example of calling the static method and binding the
// resultant Dataset to a DropDown. The nice thing about this is
// that you can sort it before binding.
DataSet binder = Helpers.Convert ObjectArrayToDa taSet(objs);
binder.Tables[0].DefaultView.So rt = "ClientName ";
dropDown.DataSo urce = binder.Tables[0].DefaultView;
dropDown.DataVa lueField = "ID";
dropDown.DataTe xtField = "ClientName ";
dropDown.DataBi nd();

"fd537" wrote:
Hi,

I was searching the internet to find out how to bind the selected
columns of an array of custom objects to the datagrid and i found this
post on google groups. I want you to please have a look at my sample
code and let me know if i am on the right track or not. I am new to
..net and would like to have some guidance from u guys. here is what
iam trying to do:

1) get an array of objects from the web service method(for ex: lets
say there are 4 fields/properties for each object)
2) i want to bind only 2 fields/properties to the datagrid.
3) i have written an event handler that assigns the value of the
property to the literal control inside a datagrid's item template
column.
4) attached the event handler to the datagrid by using the
OnItemDataBound event of the datagrid
here is the code that i have:

event handler for the OnItemDataBound event :

public void Item_Bound(obje ct sender, DataGridItemEve ntArgs e)
{
if(e.Item.ItemT ype == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
Literal l = (Literal)e.Item .FindControl("l it");
if(l.Text == String.Empty)
{
l.Text = ((FormsOrdering WS.Order)
e.Item.DataItem ).OrderDate.ToS tring();
}
}
}

HTML code for datagrid:

<asp:DataGrid OnItemDataBound ="Item_Bound".. ....>
<asp:Columns>
<asp:TemplateCo lumn HeaderText="Ord er Date">
<ItemTemplate >
<asp:Literal ID="lit" Runat="server"> </asp:Literal>
</ItemTemplate>
</asp:TemplateCol umn>
</asp:Columns>
</asp:DataGrid>
please suggest changes if iam wrong. Thank you

Nov 19 '05 #8

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

Similar topics

7
3261
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
2
2094
by: Pete Nelson | last post by:
Does anyone know of a way to bind a string collection to a datagrid? Basically, I have an object, CreditPlans, and a string collection, ModelNumbers. I'd like to bind the ModelNumbers collection to the datagrid, but because there isn't a custom object with properties, just a string object, I'm not sure how to reference it in the datagrid. FYI, I can enumerate through the collection... foreach(string modelNum in...
3
1944
by: AFN | last post by:
In VB.NET, I can successfully bind a generic datagrid to a 1 dimensional array. But, now I want to add a button column, and so I think I need to make the first column (the one having data in the array) a "BoundColumn" and the 2nd column my button column. The problem is that the BoundColumn wants a DataField name. There is none! So how do I bind this one column to the array column when the array doesn't have column names? And if your...
2
2203
by: A Traveler | last post by:
Hi, I have a custom collection class i wrote, LineItemsCollection, which is a strongly typed collection of objects of my LineItem class. The LineItem class is a simple class with just a couple properties, ProdID, Description, Quantity and UPC. 3 strings and a long. The LineItemsCollection class inherits from System.Collections.Specialized.NameObjectCollectionBase. It also implements both IEnumerable and IList.
1
1273
by: Luis Esteban Valencia | last post by:
Please everybody participate in this question. Hello my applicacion has many layers and classes and I think its well structured. PLease let me know if you disagree and why. 1. I have the user Interface (webforms) 2. I have a class Called Customer Class (it only has private fields, properties). Those properties connecto a Class called CustomerDALC (Customer Data Access Logic Component). this one must be the only responsible to access...
0
2562
by: Giorgio | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a specified date. - DeleteProfile. Enables you to delete a profile associated with a
0
1952
by: a | last post by:
Q. Unable to get a Profile custom (object) collection to bind to GridView,etc (IList objects)? This is my first custom object so I may be doing something rather simple, wrong, or it may be something else to do with the Profile object. Either way, I need help Here's a brief description of the code----------------------------------------------------------------------------------
0
1350
by: sean worlock | last post by:
Hello all, I have googled like crazy for this with no sucess! I have a dataset containing a table with the following fields Table===Widths --------------------------------------------- WidthID---int (primary key) Width------decimal
17
7247
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
9148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9012
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8855
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3034
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1992
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.