Databinding a Web UserControl

A

Allan Ebdrup

I have a Web User Control (.ascx page) wherin i have a repeater, now I would
like to databind to the Web User Control, and have it databind to the
repeater in the control. Ive taken a look at the walkthroughs at:
http://msdn2.microsoft.com/en-us/library/ms233813.aspx
http://msdn2.microsoft.com/en-us/library/ms171926.aspx

but can't really see what would be the best solution for this kind of nested
databinding, I mean what If I have several repeaters in my Web User Control
that I want to databind seperately, what would be the best practice for
implementing this kind of nested databinding?

It is important that I encapsulate the databinding in my Web User Control so
that I can replace it with another Web User Control that has the same
databinding behaviour, and that way make several Web User Controls that I
can switch on the main page seamlessly without having to modify anything on
the main page other than use a different Web User Control.

Any Ideas?

Kind Regards,
Allan Ebdrup
 
W

Walter Wang [MSFT]

Hi Allan,

Thank you for posting!

For Complex Data Binding, the attribute ComplexBindingProperitesAttribute
can only be used to specify one DataSource/DataMember. But this attribute
is only required by Designer, not mandatory for implementing data binding
user controls.

Based on my understanding, we can here simply expose those Repeaters'
DataSource property individually. For example:

==========
Create a Web User Control and add two repeaters:

public partial class WebUserControl : System.Web.UI.UserControl
{
public object DataSource1
{
get { return Repeater1.DataSource; }
set { Repeater1.DataSource = value; }
}

public object DataSource2
{
get { return Repeater2.DataSource; }
set { Repeater2.DataSource = value; }
}
}

==========
When using this user control, specify two data sources by code, then call
DataBind:

WebUserControl1.DataSource1 = ds1;
WebUserControl1.DataSource2 = ds2;
WebUserControl1.DataBind();

==========

Hope this helps.



Regards,
Walter Wang
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.
 
A

Allan Ebdrup

Walter Wang said:
For Complex Data Binding, the attribute ComplexBindingProperitesAttribute
can only be used to specify one DataSource/DataMember. But this attribute
is only required by Designer, not mandatory for implementing data binding
user controls.

Based on my understanding, we can here simply expose those Repeaters'
DataSource property individually. For example:

==========
Create a Web User Control and add two repeaters:

public partial class WebUserControl : System.Web.UI.UserControl
{
public object DataSource1
{
get { return Repeater1.DataSource; }
set { Repeater1.DataSource = value; }
}

public object DataSource2
{
get { return Repeater2.DataSource; }
set { Repeater2.DataSource = value; }
}
}

==========
When using this user control, specify two data sources by code, then call
DataBind:

WebUserControl1.DataSource1 = ds1;
WebUserControl1.DataSource2 = ds2;
WebUserControl1.DataBind();

Yes that did the trick.
I also had to define a interface that both Web User Controls implement and
use Page.FindControl to find the swapped control and cast it to the
interface.
But now it works, I can programatically switch a Web User Control on the
page with another.

Thanks,
Allan Ebdrup
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top