Databinding a Web UserControl

Discussion in 'ASP .Net Web Controls' started by Allan Ebdrup, May 19, 2006.

  1. Allan Ebdrup

    Allan Ebdrup Guest

    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
    Allan Ebdrup, May 19, 2006
    #1
    1. Advertising

  2. 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.
    Walter Wang [MSFT], May 22, 2006
    #2
    1. Advertising

  3. Allan Ebdrup

    Allan Ebdrup Guest

    "Walter Wang [MSFT]" <> wrote in message
    news:%...
    > 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
    Allan Ebdrup, May 22, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Wright via .NET 247
    Replies:
    2
    Views:
    665
    Brian W
    Jun 12, 2004
  2. Replies:
    0
    Views:
    462
  3. jobs
    Replies:
    0
    Views:
    1,591
  4. Earl Teigrob
    Replies:
    1
    Views:
    385
    Shravan
    Mar 1, 2004
  5. Kan Grewal

    Web Usercontrol in a Web Usercontrol not displaying...

    Kan Grewal, Jul 6, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    110
    Kan Grewal
    Jul 7, 2004
Loading...

Share This Page