ComponentOne ToolBar for ASP.NET AJAX: ToolBar for ASP.NET AJAX Task-Based Help > Bind C1ToolBar to an AccessDataSource

Bind C1ToolBar to an AccessDataSource

This lesson shows you how to bind C1ToolBar to a DataView by using the AccessDataSource to connect the Microsoft Access DataBase file to the C1ToolBar control and then using code to create the hierarchy for the C1ToolBar control. The hierarchy is created by using ID->ParentID relation.

Once the C1ToolBar control is bound, the hierarchy is determined, you can set the Text and ToolTip properties.

Note: The toolbar uses data from the sample Access database, toolbardata.mdb, included in the ControlExplorer Visual Studio sample project.

The final application will appear like the following at run time:

To bind the C1ToolBar control to the DataView, complete the following:

1.   Start a new Web Site.

2.   Copy the toolbardata.mdb database file from the App_Data folder from the ControlExplorer sample project to the App_Data folder in your project.

3.   Save and close the project then reopen it so you'll see that the toolbardata.mdb file appears in your App_Data folder.

4.   Expand the Data node in the Visual Studio Toolbox and add the AccessDataSource component to the Web page using a drag-and-drop operation.

5.   Right-click on the AccessDataSource control and select Properties to bring its properties to focus.

6.   Click on the ellipsis button next to the DataFile property to open the Select Microsoft Access Database dialog box. Select the App_Data and click on the toolbardata.mdb file. Click OK to add it to the AccessSource1.DataFile property.

 

 

7.   Toggle to Source view and add the SelectCommand to retrieve the data from the LINKS table.

<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/toolbardata.mdb" SelectCommand="SELECT * FROM [LINKS]"></asp:AccessDataSource>

8.   Add the C1ToolBar control to your page.

9.   To specify the namespaces used in this example, add the following statements before any declarations in the Code Editor:

      Visual Basic

Imports C1.Web.UI.Controls.C1ToolBar

Imports System.Data;

Imports System.Reflection;

      C#

using C1.Web.UI.Controls.C1ToolBar;

using System.Data;

using System.Reflection;

10.  Add the following code in your code behind page to connect the C1ToolBar control to the AccessDataSource, select the DataView, establish hierarchy and set the Text property:

      Visual Basic

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    CreateToolBarItemsFromAccessDataSourceExample(C1ToolBar1, Me.AccessDataSource1)

End Sub

 

Protected Sub CreateToolBarItemsFromAccessDataSourceExample(ByVal toolBar As C1ToolBar, ByVal dataSource As SqlDataSource)

    ' Select DataView:

    Dim dsArgs As New DataSourceSelectArguments()

    Dim dataView As DataView = TryCast(dataSource.[Select](dsArgs), DataView)

   

    ' sort by parent ID:

    dataView.Sort = "ParentId"

   

    ' Clear old toolbar items:

    toolBar.Items.Clear()

   

    ' Create ToolBar Items according ToolBarItemID/ParentToolBarItemID relationship.

   

    Dim toolbarItemsDict As New Dictionary(Of String, IC1ToolBarItemCollectionOwner)()

   

    For i As Integer = 0 To dataView.Count - 1

        Dim row As DataRow = dataView(i).Row

        Dim item As IC1ToolBarItemCollectionOwner = Me.CreateToolBarItem(row)

       

        Dim itemID As String = row("id").ToString()

        Dim parentID As String = row("ParentId").ToString()

        toolbarItemsDict.Add(itemID, item)

       

        If parentID = "" Then

            ' this is root toolbar item, add it to toolbar:

            toolBar.Items.Add(item)

        Else

            If toolbarItemsDict.ContainsKey(parentID) Then

                ' this is child toolbar item, add it to parent toolbar item:

                toolbarItemsDict(parentID).Items.Add(item)

            End If

        End If

    Next

End Sub

 

Private Function CreateToolBarItem(ByVal row As DataRow) As IC1ToolBarItemCollectionOwner

    Dim typeName As String = (GetType(C1ToolBarItem).[Namespace] & ".") + row("ToolBarItemType").ToString()

    Dim assembly__1 As Assembly = Assembly.GetAssembly(GetType(C1ToolBarItem))

    Dim type As Type = assembly__1.[GetType](typeName)

   

    Dim c1ToolBarItem As C1ToolBarItem = TryCast(Activator.CreateInstance(type), C1ToolBarItem)

    c1ToolBarItem.Text = row("Text").ToString()

   

    Return c1ToolBarItem

End Function

      C#

protected void Page_Load(object sender, EventArgs e)

{

       CreateToolBarItemsFromAccessDataSourceExample(C1ToolBar1, this.AccessDataSource1);   

}

 

protected void CreateToolBarItemsFromAccessDataSourceExample(C1ToolBar toolBar, SqlDataSource dataSource)

{

    // Select DataView:

    DataSourceSelectArguments dsArgs = new DataSourceSelectArguments();

        DataView dataView = dataSource.Select(dsArgs) as DataView;

 

    // sort by parent ID:

    dataView.Sort = "ParentId";

 

    // Clear old toolbar items:

    toolBar.Items.Clear();

 

    // Create ToolBar Items according ToolBarItemID/ParentToolBarItemID relationship.

 

    Dictionary<string, IC1ToolBarItemCollectionOwner> toolbarItemsDict = new Dictionary<string, IC1ToolBarItemCollectionOwner>();

 

    for (int i = 0; i < dataView.Count; i++)

    {

        DataRow row = dataView[i].Row;

        IC1ToolBarItemCollectionOwner item = this.CreateToolBarItem(row);

 

        string itemID = row["id"].ToString();

        string parentID = row["ParentId"].ToString();

        toolbarItemsDict.Add(itemID, item);

 

        if (parentID == "")

        {

            // this is root toolbar item, add it to toolbar:

            toolBar.Items.Add(item);

         }

        else

        {

            if (toolbarItemsDict.ContainsKey(parentID))

            {

                // this is child toolbar item, add it to parent toolbar item:

                toolbarItemsDict[parentID].Items.Add(item);

            }

        }

    }

}

 

private IC1ToolBarItemCollectionOwner CreateToolBarItem(DataRow row)

{

    string typeName = typeof(C1ToolBarItem).Namespace + "." + row["ToolBarItemType"].ToString();

    Assembly assembly = Assembly.GetAssembly(typeof(C1ToolBarItem));

    Type type = assembly.GetType(typeName);

 

    C1ToolBarItem c1ToolBarItem = Activator.CreateInstance(type) as C1ToolBarItem;

    c1ToolBarItem.Text = row["Text"].ToString();

 

    return c1ToolBarItem;

    }

This Topic Illustrates the Following:

Run the project and notice the data is bound to the C1ToolBar control.

 


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.