Chris Cavanagh’s Blog

ASP.NET InlineDataSource

December 14, 2007 · 5 Comments

Here’s a very simple ASP.NET data source control, useful if you want to bind to data dynamically generated in code-behind.  It behaves just like ObjectDataSource but expects to get all its data through event handlers you attach (typically defined on the same control or page).

Here’s an example of how it might be used:

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Register TagPrefix="cjc" Namespace="CJC.Web.UI" %>

<asp:DropDownList ID="myList" DataSourceID="myShapedData" runat="server"
    DataTextField="Text" DataValueField="Value" />

<cjc:InlineDataSource ID="myShapedData" runat="server"
    OnSelect="myShapedData_Select" />

<script language="C#" runat="server">
    private IEnumerable<int> GetSomeData()
    {
        return new int[] { 0, 6, 3, 2, 1, 7, 9 };
    }

    private string Lookup( int key )
    {
        switch ( key )
        {
            case 1: return "Thing 1";
            case 3: return "Thing 2";
            case 7: return "Thing 3";
            default: return "I dunno";
        }
    }

    private void myShapedData_Select(
        object sender,
        InlineDataSourceSelectEventArgs e )
    {
        e.ReturnValue = from s in GetSomeData()
                        select new
                        {
                            Text = Lookup( s ),
                            Value = s
                        };
    }
</script>

 

You can download InlineDataSource right here.

Categories: .NET

5 responses so far ↓

Leave a Comment