Export (0) Print
Expand All

How to: Create Table Action Links in Dynamic Data

.NET Framework 4

ASP.NET Dynamic Data enables you to create dynamic hyperlinks that are based on the table actions that are enabled by Dynamic Data. When a user clicks a link, a page is displayed that enables the user to perform the action that is specified by the link, such as listing detail data rows or editing a data item.

This topic shows you how to create Dynamic Data dynamic hyperlinks by using the DynamicHyperLink control in the following scenarios:

  • Table-row action links for data-bound controls. These are action links that are specific to data rows in a data-bound control, such as "Edit" or "Details". Dynamic Data infers the table and the primary key from the row itself.

  • Meta-table action links for data-bound controls. These are MetaTable action links that are specific to tables in a data-bound control, such as "List" or "Insert".

  • Unbound action links. These are action links that are not specific to a data-bound control. For these links, you must define the table to access and the action to perform.

To create data-bound control table row action links

  1. In Visual Studio 2010 or Visual Web Developer 2010 Express, create or open a Dynamic Data Web site.

  2. Create or open an ASP.NET Web page.

  3. Add a DynamicDataManager control to the page, as shown in the following example:

    <body>
      <form ID="Form1" runat="server">
        <asp:DynamicDataManager ID="DynamicDataManager1"
          runat="server"/>
      </form>
    </body>
    

    The DynamicDataManager control must be included on a page in order to support Dynamic Data controls. The markup for the DynamicDataManager control must precede the markup for any controls that use Dynamic Data.

  4. Add a data source control to the page and configure it to access the data table in which to create the action links.

    The following example shows the markup for a LinqDataSource control, which is configured to access the Products table from the AdventureWorksLT database.

    <asp:LinqDataSource ID="LinqDataSource1"  runat="server"  
      ContextTypeName="AdventureWorksLTDataContext"
      TableName="Products"/>
    
  5. Add a data-bound control that supports templates.

    The following example shows the markup for a GridView control that is configured to display data from the LinqDataSource control.

    <asp:GridView ID="GridView1" DataSourceID=" LinqDataSource1" runat="server">
    </asp:GridView>
    
  6. Add a DynamicHyperLink control to the TemplateField template of the data-bound control.

    The following example shows the markup that creates an Edit link for each row in the associated table.

    <asp:GridView ID="GridView1" DataSourceID="LinqDataSource1" 
        runat="server">
      
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:DynamicHyperLink runat="server" 
              ID="EditHyperLink" 
              Action="Edit" 
              Text="Edit"/>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
      
    </asp:GridView>
    

    When the user clicks the Edit link, Dynamic Data will display a page that uses the Edit.aspx default page template to render the UI for editing the related row.

  7. Register the data-bound control with the DynamicDataManager control, as shown in the following example:

    <asp:DynamicDataManager ID="DynamicDataManager1"
      runat="server">
      <DataControls>
        <asp:DataControlReference ControlID="GridView1"/>
      <DataControls>
    </asp:DynamicDataManager>
    

    The DynamicDataManager control instructs Dynamic Data to handle the registered data-bound controls as dynamic controls.

  8. Save and close the page.

To create data-bound control meta-table action links

  1. In Visual Studio 2010 or Visual Web Developer 2010 Express, create or open a Dynamic Data Web site.

  2. Create or open an ASP.NET Web page. Make sure that the page has a related code-behind class file.

  3. Add a DynamicDataManager control to the page, as shown in the following example:

    <body>
      <form ID="Form1" runat="server">
        <asp:DynamicDataManager ID="DynamicDataManager1"
          runat="server"/>
      </form>
    </body>
    

    The DynamicDataManager control must be included on a page in order to support Dynamic Data controls. The markup for the DynamicDataManager control must precede the markup for any controls that use Dynamic Data.

  4. Add a data source control to the page and configure it to access the collection that contains database tables to link to.

    The following example shows the markup for a LinqDataSource control that is configured to access the tables contained in the AdventureWorksLT database.

    <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
      ContextTypeName="AdventureWorksLTDataContext">
    </asp:LinqDataSource>
    
  5. Add a data-bound control that supports templates.

    The following example shows the markup for a GridView control, which is configured to display data from the LinqDataSource control named LinqDataSource1.

    <asp:GridView ID="GridView1" DataSourceID="LinqDataSource1" runat="server">
    </asp:GridView>
    
  6. Add a DynamicHyperLink control to the TemplateField template of the data-bound control.

    The following example shows the markup that creates links for each table in the collection.

    <asp:GridView ID="GridView1" DataSourceID="LinqDataSource1" runat="server"/> 
      <Columns>
        <asp:TemplateField HeaderText="Table Name" >
          <ItemTemplate>
            <asp:DynamicHyperLink ID="DynamicHyperLink1" 
                runat="server"
                TableName='<%# Eval("Name") %>'>
              <%# Eval("DisplayName") %> 
            </asp:DynamicHyperLink>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns> 
    </asp:GridView>
    
  7. Register the data-bound control with the DynamicDataManager control, as shown in the following example:

    <asp:DynamicDataManager ID="DynamicDataManager1"
      runat="server">
      <DataControls>
        <asp:DataControlReference ControlID="GridView1"/>
      <DataControls>
    </asp:DynamicDataManager>
    

    The DynamicDataManager control instructs Dynamic Data to handle the registered data-bound controls as dynamic controls.

  8. Save and close the page.

  9. Open the code-behind class file.

  10. In the Page_Load method, bind the data-bound control to the IList collection of tables that is available in Dynamic Data.

    The following example shows how to bind the GridView1 control to the IList collection of tables.

    protected void Page_Load(object sender, EventArgs e)
    {
        If (!IsPostBack)
        {
          // Get the list of visible tables.
          System.Collections.IList tables =
              ASP.global_asax.model.VisibleTables;
          // Throw an exception if there are no visible tables.
          if (tables.Count == 0){ 
            throw new InvalidOperationException();
          }
          // Bind the data-bound control to 
          // the list of tables.
          GridView1.DataSource = tables;
          GridView1.DataBind();
        }
    }
    

    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As EventArgs)
        If Not IsPostBack Then
          ' Get the list of visible tables.
          Dim tables As System.Collections.IList = _
            ASP.global_asax.L2Smodel.VisibleTables
          ' Throw an exception if there are no visible tables.
          If tables.Count = 0 Then
            Throw New InvalidOperationException()
          End If
          ' Bind the data-bound control to 
          ' the list of tables.
          GridView1.DataSource = tables
          GridView1.DataBind()
        End If
    End Sub
    
  11. Save and close the code-behind file.

To create unbound action links

  1. In Visual Studio 2010 or Visual Web Developer 2010 Express, create or open a Dynamic Data Web site.

  2. Create or open an ASP.NET Web page.

  3. Add a data source control to the page and configure it to access the data tables that you want to operate on.

    The following example shows the markup for a LinqDataSource control that accesses tables that are contained in the AdventureWorksLT database.

    <asp:LinqDataSource ID="LinqDataSource1"  runat="server"  
      ContextTypeName="AdventureWorksLTDataContext">
    </asp:LinqDataSource>
    
  4. Add a DynamicHyperLink control to the page and configure it as an unbound link.

    An unbound link refers to an action link that is not part of a data-bound control. For these links, you must define the table to access and the action to perform.

    <asp:DynamicHyperLink ID="DynamicHyperLink1">
        Insert new item
    </asp:DynamicHyperLink>
    
  5. Set the Action property of the DynamicHyperLink control to the action you want to perform.

    The following example shows how to set the Action property to Insert.

    <asp:DynamicHyperLink ID="DynamicHyperLink1" 
        Action="Insert">
      Insert new item
    </asp:DynamicHyperLink>
    
  6. Set the TableName property of the DynamicHyperLink control to the name of the table that you want to act on.

    The following example shows how to set the TableName property to Products

    <asp:DynamicHyperLink ID="DynamicHyperLink1" 
        Action="Insert" TableName= "Products">
      Insert new item
    </asp:DynamicHyperLink>
    

    When the user clicks the Insert link, Dynamic Data displays a page that uses the Insert.aspx page template to render the UI for inserting a new item.

The following example creates dynamic hyperlinks for these scenarios:

  • Table-row action links for data-bound controls. The example creates Edit links for each Products table data row. When the user clicks any Edit link, Dynamic Data uses the Edit.aspx page template to render the UI for editing the related row.

  • Meta-table action links for data-bound controls. The example creates links that display the collection of tables that are contained in the AdventureWorksLT sample database. When the user clicks a link, Dynamic Data uses the List.aspx page template to render the UI for displaying the related table.

  • Unbound action links. The example creates Edit and Insert links for the Products table that are not part of any data-bound control. The example specifies the table to access and the action to perform. In the case of the Edit link, it also specifies the primary key for the row to modify.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicDataDynamicHyperlink.aspx.cs" Inherits="DocSamples_DynamicDataDynamicHyperlink" %>

<%@ Register src="~/DynamicData/Content/GridViewPager.ascx" tagname="GridViewPager" tagprefix="asp" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dynamic Hyperlinks</title>
</head>
<body>
    <form id="form1" runat="server">

    <asp:DynamicDataManager  ID="DynamicDataManager1" 
        runat="server">
        <DataControls>
            <asp:DataControlReference ControlID="GridView1" />
        </DataControls>
    </asp:DynamicDataManager>

    <div>
        <h3>Dynamic Hyperlinks</h3>

         <h4>UnBound Control Action Links</h4>

        <asp:DynamicHyperLink ID="InsertHyperLink" runat="server" 
            Action="Insert"   
            TableName="ProductModels">Insert new item
        </asp:DynamicHyperLink>

        <br />

        <asp:DynamicHyperLink ID="DynamicHyperLink2" runat="server" 
            Action="Edit" 
            TableName="ProductModels"  
            ProductModelID="1">Edit item</asp:DynamicHyperLink>

        <h4>Data-Bound Control Meta-Table Action Links</h4>

        <asp:GridView ID="GridView2" runat="server" 
            AutoGenerateColumns="false"
            CellPadding="6">
            <Columns>
               <asp:TemplateField HeaderText="Table Name" 
                    SortExpression="TableName">
                    <ItemTemplate>
                        <asp:DynamicHyperLink ID="DynamicHyperLink1" 
                            runat="server"  
                            OnDataBinding="DynamicHyperLink_DataBinding"
                            TableName='<%# Eval("Name") %>'>
                                <%# Eval("DisplayName") %>
                        </asp:DynamicHyperLink>     
                    </ItemTemplate> 
                </asp:TemplateField>    
            </Columns>
        </asp:GridView>


        <h4>Data-Bound Control Table Row Action Links</h4>

        <asp:GridView ID="GridView1" runat="server" 
            AllowPaging="true" PageSize="5"
            DataSourceID="LinqDataSource1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DynamicHyperLink ID="EditHyperLink" 
                            runat="server"  
                            Action="Edit" Text="Edit" />       
                    </ItemTemplate> 
                </asp:TemplateField>    
            </Columns>
        </asp:GridView>



       <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="AdventureWorksLTDataContext"
            TableName="Products"/>

    </div>
    </form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.DynamicData;

public partial class DocSamples_DynamicDataDynamicHyperlink : 
    System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Get the list of visible tables.
            System.Collections.IList tables =
                ASP.global_asax.model.VisibleTables;

            // Throw an exception if there are no visible tables.
            if (tables.Count == 0)
            {
                throw new InvalidOperationException();
            }

            // Bind the data-bound control to 
            // the list of tables.
            GridView2.DataSource = tables;
            GridView2.DataBind();
        }
    }

    protected void DynamicHyperLink_DataBinding(object sender,
        EventArgs e)
    {
        MetaTable table = (MetaTable)GetDataItem();
        DynamicHyperLink dynamicHyperLink =
            (DynamicHyperLink)sender;
        // Set the context type name for the DynamicHyperLink 
        dynamicHyperLink.ContextTypeName =
            table.DataContextType.AssemblyQualifiedName;
    }
}


This example requires the following:

Community Additions

ADD
Show:
© 2014 Microsoft