views:

621

answers:

3

If I create a user control (EDIT:not a web control/server control) it's pretty trivial to get databinding. I just add a datasourceID property.

In code behind (vb)

Partial Public Class BandedControl
    Inherits UserControl

    Public Property DataSourceID() As String
        Get
            Return MyGridView.DataSourceID
        End Get
        Set(ByVal value As String)
            MyGridView.DataSourceID = value
        End Set
    End Property

End Class

In code behind (c#)

public partial class BandedControl : UserControl
{

    public string DataSourceID {
        get { return MyGridView.DataSourceID; }
        set { MyGridView.DataSourceID = value; }
    }
}

My issue is that this breaks design time rendering and also I don't get a drop down list to choose my datasource. How do I resolve this. (Hint: I think I need a type convertor, but all the info I can find relates to server controls not user controls).

+1  A: 

Web UserControls are compiled dynamically at run time and so are not rendered at Design time, what you want to do is create a Web Custom Control. Your best bet here is to extend one of the existing Bindable Web Controls

http://msdn.microsoft.com/en-us/library/aa651710(VS.71).aspx

CalvinR
Thanks for looking. But if the datasource is inside the user control then the control *is* rendered on the design surface in VS2008 (I think this is a new VS2008 feature). This article refers to VS.NET (2003), so I may possibly still be able to do this with user controls... ???
Christopher Edwards
Sorry I don't have exp with vs2008 but honestly I think it may be easier for you to spend the time and create a custom control, then to wait for an answer. Maybe this will help http://weblogs.asp.net/scottgu/archive/2005/12/04/432319.aspx
CalvinR
There is no imminent need for this, I start a month long (hopefully!) web project in March, I currently doing winforms stuff which I have more experience at and I'm just feeling out my options. I'll wait for other answers and if it can't be done (or no one knows how to do it) then I'll reconsider.
Christopher Edwards
+2  A: 

You could try adding the IDReferenceProperty attribute to your property definition...

public partial class BandedControl : UserControl
{
    [System.Web.UI.IDReferenceProperty(typeof(DataSourceControl))]
    public string DataSourceID
    {
        get
        {
           return MyGridView.DataSourceID;
        }
        set
        {
           MyGridView.DataSourceID = value; 
        }
    }
}

See http://msdn.microsoft.com/en-us/library/system.web.ui.idreferencepropertyattribute.aspx for more info about the IDReferencePropertyAttribute class.

If that doesn't work - I'd also try to inherit from DataBoundControl instead of UserControl and see if that gets you anywhere.

Scott Ivey
Thanks, but that doesn't work for user controls, which is what I'm trying to use. If I inherit from DataBoundControl I'll no longer be a user control and I'll lose the ability to use the control designer, which is what I'm trying to avoid.
Christopher Edwards
Yeah - i think you'll need to do it as a TypeConverter. Maybe a custom typeconverter that will pull the datasource controls from the page that its on rather than from the control itself.
Scott Ivey
A: 

Not sure if this is exactly what you want but I seem to remember them showing something similar to this in some dnr tv episodes.

I think it was Miguel Castro episodes 1 & 2, but it could be episode 31.

An archive of all the videos is here

Remmus
Thanks I'd viewing them now. But I think these are about web controls/server controls not user controls....
Christopher Edwards
I'm pretty sure you can't do what you want with a user control and have to use web / server control
Remmus
Yeah, I'm reaching the same conclusion. Which is a shame.
Christopher Edwards