How to get the data from an ObjectDataSource in a custom control

R

Rolf Welskes

Hello,
I have an ObjectDataSource which has as business-object a simple array of
strings. No problem.

I have an own (custom) control to which I give the DataSourceId and in the
custom-control so I get the ObjectDataSource. No problem

.....
ObjectDataSource src = .... //is ok i have it


Now I want

foreach(..... row .... in the list of data in src)

render the row .


but I do not know how to get the rows of data of the
ObjectDataSource-object.

Any help would be fine.

Thank you

Rolf Welskes
 
L

Luke Zhang [MSFT]

Hello Rolf,

Would you please show use how you define the class for the business-object,
this may help us find a way to loop the string item in it.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Justin Huang

Hello Rolf,

Thank you for posting.

From your post, my understanding on this issue is:List Rows data listed in
ObjectDataSource. If I'm off base, please feel free to let me know.

Please take a glance about the ObjectDataSource:
http://msdn2.microsoft.com/en-us/library/9a4kyhcx.aspx

ObjectDataSource is not of that usage.

Sincerely,
Justin Huang
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Rolf Welskes" <[email protected]>
| Subject: How to get the data from an ObjectDataSource in a custom control
| Date: Tue, 3 Oct 2006 22:22:06 +0200
| Lines: 29
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: p5090F901.dip.t-dialin.net 80.144.249.1
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP06.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:422396
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hello,
| I have an ObjectDataSource which has as business-object a simple array of
| strings. No problem.
|
| I have an own (custom) control to which I give the DataSourceId and in
the
| custom-control so I get the ObjectDataSource. No problem
|
| ....
| ObjectDataSource src = .... //is ok i have it
|
|
| Now I want
|
| foreach(..... row .... in the list of data in src)
|
| render the row .
|
|
| but I do not know how to get the rows of data of the
| ObjectDataSource-object.
|
| Any help would be fine.
|
| Thank you
|
| Rolf Welskes
|
|
|
|
 
R

Rolf Welskes

Hello,
thank you for your answer.

right if I could list the data in an ObjectDataSource, I could develop all
other.

You can see any simple business-object as the holder of the data
for example
[DataObject(true)]
public class DataObjCls01
{
string[] datas01 = new string[] { "Müller", "Meyer", "Schulze" };

public DataObjCls01()
{
}

[DataObjectMethod(DataObjectMethodType.Select)]
public string[] GetDatas01()
{
return datas01;
}

}

So I need the prinzipial access to the data in a custom control.

Thank you and best regards
Rolf Welskes
 
L

Luke Zhang [MSFT]

Hello Rolf,

With such a definition, you can loop in the array like this:

DataObjCls01 oc = new DataObjCls01();

foreach (string s in oc.GetDatas01() )
{
Response.Write(s);
}

Sincerely,

Luke Zhang

Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
this is not the question.

In my control I have only the ObjectDataSource.
And the question is how to get access to the business-object by using the
ObjectDataSource.

In details:
In the page I have an ObjectDataSource.
The ObjectDataSource has as BusinessObjekt DataObjCls01
As SelectMethod: GetData() of the BusinessObject.

The customControl has as DataSourceId the ObjectDataSource.

So it is no problem in the CustomControl to get the ObjectDataSource-Object.

But whats now:
in CustomControl I have now
ObectDataSource theObjectDataSource = .... .
//no problem I have it.

The custom-Control knows nothing about the Business-Object.
This should allways be the case because of abstraction (why use
ObjectDataSource if no abstraction).

So, from theObjectDataSource I can get the Name of the SelectMethod
(GetData) and
I can get the Type of the BusinessObjekt.
But to make a call of GetData (with reflection clearly) I need a little
more, for example the object (not only the type)
of the Business-object.

Clearly, I have to make a call of GetData by reflection (because I do not
know the type of the BusinessObject).
But how to get what I need from theObjectDataSource ?

Thank you for your help.
Rolf Welskes
 
R

Rolf Welskes

Hello,
I have thought about this.

I think we are on the wrong way.

If I have a DataSourceObject which has DataObjCls01 as business-object
with GetData as Select-Method.
THEN
When I want to use data in a customcontrol of the ObjectDataSource
I have NOT to call anyway GetData(). GetData is called from the
ObjectDataSource.

So the ObjectDataSource-Object must give me the data-rows which it has get
with GetData().

but how?

Think like this: If I have a Listbox I can bind it to the ObjectDataSource.
The listbox knows nothing about the used Business-Object.

So if I have my own customControl (mybe also a listbox) I must have access
to the Data of the ObjectDataSource
WITHOUT to know anything about the business-object.

So it works for listBox (means ListBox implements an access to the
ObjectDataSource).
This I need to know how?

In the msdn ObjectDataSource is only documented for use with existing
Controls, nothing about, how to
use ObjectDataSource in own build controls.

Thank you for any help.
Rolf Welskes
 
L

Luke Zhang [MSFT]

Hello,

From the code:

[DataObjectMethod(DataObjectMethodType.Select)]
public string[] GetDatas01()
{
return datas01;
}

Will it only return an array of string, not a business object? Is there any
thing I missed here?

Also, if want to access the business object in the customcontrol, you need
to refer the business class project, and access its public properties.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
right in this test class
we have
[DataObjectMethod(DataObjectMethodType.Select)]
public string[] GetDatas01()
{
return datas01;
}

But how to right code to use it form the ObjectDataSource without knowing
something of the business-object ?
I want to do the same as a normal ListBox does when bound to the
ObjectDataSource
and this knows nothing about the business-object.
Ok it woudl be good also to get the business-object as object (for
reflection work).

But the most important is:
Get the data from the ObjectDataSource as a ListBox do.

Thank you.
Rolf Welskes
 
R

Rolf Welskes

Hello,
is realy no one able to help me and give me a hint what to do?

In my last reply I have described precisly what the problem is.

Thank you for any help.

Rolf Welskes
 
S

Steven Cheng[MSFT]

Hello Rolf,

I'm sorry for keep you waiting so long. We just found this problem thread
when reviewing the newsgroup and it seems our internal tool has incorrectly
missed this issue.

I'll continue to help you on this thread. After a overview through the
previous messages, I'd like to confirm you with my understanding on your
question here:

You're developing a custom webserver control which will use "DataSourceID"
to reference a DataSourceControl(just like any other built-in databound
control such as GridView, listbox....), and you're wondering how to get the
list of dataitems from DataSourceControl and construct your control's
output control tree (when perform databinding), correct? If there is
anything I missed, please corret me.

Based on my research, you can consider the two approaches in your custom
webcontrol to retrieve data list from datasource control:

1. ASP.NET 2.0 has already provided a built-in class "DataBoundControl" for
developing webcontrol that will use the new DataSourceControl databinding
model.

#DataBoundControl Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.databound
control.aspx

You just need to derive your control from "DataBoundControl" class, this
class has already implement the "DataSourceID" property and other methods
necessary for querying data from referenced DataSourceControl(such as the
"GetData" and "GetDataSource"), you can directly call them to get data list
from connected datasource control in your own control's code.

Here is another msdn article describing create a custom databound control
through the above super class:



#Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0
http://msdn2.microsoft.com/en-US/library/ms366539.aspx



2. In #1, I mentioned use the "DataBoundControl" super class to make our
custom control directly get the ability to manipulate data from datasource
control. You can also do it your self if you do not want to derive your
control from the DataBoundControl class. In this case, you need to do the
following things in your control's code:

** Use FindControl method to locate the datasourceControl manually,
something like:

================
DataSourceControl dsc = null;

dsc = this.FindControl(this.DataSourceID) as DataSourceControl;

if(dsc == null)
dsc = this.NamingContainer.FindControl(this.DataSourceID)
as DataSourceControl;

if(dsc ==null)
throw exception........
================



** After you get the DataSourceControl, use its "IDataSource" interface to
query the certain DataSourceView in it:

#IDataSource.GetView Method
http://msdn2.microsoft.com/en-us/library/system.web.ui.idatasource.getview.a
spx

You can use the "GetViewNames" method to get avaialbe views first.


** After you get the DataSourceView, you can use its "ExecuteSelect" method
to query the underlying data(no matter use your connect to SqlDAtaSource or
ObjectDataSource)

#DataSourceView Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.datasourceview.aspx


** The DataSourceView's ExecuteSelect method will use asynchornous
pattern(use callback delegate) to return a IEnumerable list of data, you
can use it to construct the control tree in your custom control.


Hope this helps. If you have anything unclear, please feel free to let me
know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
yes, right what you say.

In the mean time I have tested to use the method
ObjectDataSource.Select().
This works.
But there is no documentation how to work with it.

Thank you and best regards
Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

Since Steven is on sick leave today, I will follow-up your question.

Any server control can be designed to automatically retrieve some of its
data from an external data source. Based on the interaction between the
data source and the control, you can identify three distinct forms of data
binding and, subsequently, data-bound controls-simple binding, list
controls, and complex binding.

Simple data binding consists of binding an object and one or more control
properties. The data source binds to an individual item as opposed to a
list of items. The internal structure of a control that uses simple binding
is nearly identical to that of a complex bound control-only simpler.

List controls are controls that display a list of data items through a
fixed and immutable user interface. Popular examples of list controls are
RadioButtonList, CheckBoxList and, new in ASP.NET 2.0, BulletList.

Complex data-bound controls are typically composite controls that display a
list of items with no limitation at all on the rendering mechanism. A good
example of complex data-bound control is the DataGrid control.

In ASP.NET 1.x, data binding works only in one direction, meaning that the
control has typically no way to update the data source. Each data-bound
control generally reads data from the data source and fires events when
something happens that requires updates on the bound source. In ASP.NET
2.0, data source controls from the underlying machinery for two-way data
binding. In the whole ASP.NET 2.0 control toolbox, only three controls
support two-way data binding-GridView, FormView, and DetailsView. Two-way
binding requires additional logic in the control code to invoke proper
methods on the bound data source, which has to be a data source control.

All data-bound controls derive from BaseDataBoundControl. The
BaseDataBoundControl class defines the machinery through which the data
binding occurs and validates any bound data. Common data source properties
are defined on this class-DataSource for enumerable data, and DataSourceID
for data source controls:

public virtual object DataSource { get; set; }
public virtual string DataSourceID { get; set; }

The DataSource property accepts objects that implements the IEnumerable
(for example, collections) or IListSource (for example, DataTable)
interface.

The DataSourceID property is a string and refers to the ID of a bound data
source control. Once a control is bound to a data source, any further
interaction between the two (in both reading and writing) is out of your
control and hidden from view.

Even though control developers are offered two distinct ways of binding,
under the hood of data-bound controls data retrieval occurs in just one
way-through data source view objects. If the control is bound to a data
source control, the incorporated data source view object is retrieved via
the members of the IDataSource interface. If the control is bound to an
enumerable object, a data source view object is dynamically built and
returned by GetData.

By design, a DataSourceView object features the Select method and returns
the bound data through it. The Select method accepts some input arguments
and a callback function. The callback function receives an enumerable
collection of data-the items to bind to the control.

The bottom line is that whatever data source you bind to the control, a
data source view object is created. A data source view object is a class
that can perform SELECT, INSERT, DELETE, and UPDATE operations on a bound
object.

Now we get to the question you initially asked: as a control developer, how
can you access this bindable collection of data?

The callback function that gets to process the results of the SELECT
operation on the data source view ends up calling a protected overridable
method-PerformDataBinding:

protected virtual void PerformDataBinding(IEnumerable data)
{
// data is the collection of data to show in the data-bound control's user
interface
}

Based on my understanding, your objective is to build a data-bound list
control. A list control builds its own user interface by repeating a fixed
template for each bound data item within the boundaries of the control's
mainframe. For example, a CheckBoxList control just repeats a CheckBox
control for each bound data item. Likewise, a DropDownList control iterates
through its data source and creates a new <option> element within a parent
<select> tag.

In ASP.NET 2.0, all list controls inherit from ListControl. ListControl
adds quite a few new members to the interface of its parent
class-DataBoundControl (which inherits from BaseDataBoundControl).

For all list controls, the data item type is ListItem. When you bind a list
control to its data, the Items collection gets filled, typically within the
PerformDataBinding method.

To demonstrate how to create a very simple list control, we will inherit
from ListControl and create a control named SimpleHyperLinkList:

public class SimpleHyperLinkList : ListControl
{
private HyperLink _controlToRepeat;
private HyperLink ControlToRepeat
{
get
{
if (_controlToRepeat == null) _controlToRepeat = new
HyperLink();
return _controlToRepeat;
}
}

protected override void Render(HtmlTextWriter writer)
{
for (int i = 0; i < Items.Count; i++)
{
HyperLink ctl = ControlToRepeat;
ctl.ApplyStyle(ControlStyle);
ctl.Text = Items.Text;
ctl.NavigateUrl = Items.Value;
ctl.RenderControl(writer);
writer.Write("<br />");
}
}
}

However, deriving your control from ListControl forces you to adopt
ListItem as the data item object (you can't build your own) and
ListItemCollection as the data item collection type. The names of the
existing mapping properties cannot be changed, though new mapping
properties can be added.

To use your own data item object and item collection, you need to derive
your control from DataBoundControl and implement the interface
IRepeatInfoUser. I can depict more on this if you need to go this way, or
you could describe your requirement more specifically so that I can suggest
you how to build such control.

By the way, I would highly recommend you read the book <<Programming
Microsoft ASP.NET 2.0 Applications: Advanced Topic>> by Dino Esposito.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you for your work.
I will check it out.
Thank you again.
Rolf Welskes
 
S

Steven Cheng[MSFT]

Thanks for the followup Rolf,

Always welcome to post here when you need any help.

Good luck:)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top