views:

192

answers:

1

I am attempting to write a custom control. I want to allow its user to specify a DataSourceId (much like GridView, Repeater, etc.).

I can get find the DataSource that corresponds to the id. And I can get the associated DataSourceView. But the only way to get the data out appears to be with an asynchonous Select method. Of course, I can set up the callback such that my code blocks until the callback occurs. But this is requiring so many weird gyrations that I suspect that I am doing something wrong.

If I want a control to function like some of the other ASP.NET data controls, what should I be doing differently?

Here's a bit that I've written:

    string dataSourceId = "SomeDataSourceForTesting";
    protected override void RenderContents(HtmlTextWriter writer)
    {
        IDataSource ds = (IDataSource)Parent.FindControl(dataSourceId);
        List<string> viewNames = new List<string>();
        foreach(string name in ds.GetViewNames())
        {
            viewNames.Add(name);
        }
        string viewname = viewNames[0];
        writer.WriteLine("the viewname is " + viewname);
        DataSourceView view = ds.GetView(viewname);
        view.Select(...); //this really has to be asynchronous?
        //write out some stuff from the data source
    }
A: 

Which type of DataBound control do you want? If you want tabular format you should derive your control from System.Web.UI.WebControls.DataBoundControl. And for hierarchical format derive from System.Web.UI.WebControls.HierarchicalDataBoundControl. Both of them have a property to assign a DataSource control.

For DataBoundControl you should override the PerformDataBinding. This method has a parameter named data that contains the provided data from DataSource.

public class Test : System.Web.UI.WebControls.DataBoundControl
{

    protected override void PerformDataBinding(System.Collections.IEnumerable data)
    {
        base.PerformDataBinding(data);
    }

}

For HierarchicalDataBoundControl you should override a method with the same name as DataBoundControl. And get view via GetData method.

public class Test : System.Web.UI.WebControls.HierarchicalDataBoundControl
{

    protected override void PerformDataBinding()
    {
        base.PerformDataBinding();

        System.Web.UI.HierarchicalDataSourceView view = base.GetData("View Path");
    }

}
Mehdi Golchin
Perfect. I figured I was missing something like this. Thanks!
Eric