DynamicHyperLink Web Server Control Declarative Syntax

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The DynamicHyperLink control creates links that can display related data or that can perform custom table actions. These actions are defined in the Global.asax file as default routing rules that are associated with default page templates such as Edit.aspx, List.aspx, and so on. When the user clicks the link, a page is displayed that enables the user to perform the action that is specified by the link.

<asp:DynamicHyperlink
    ID="string"
    Action="Details|Edit|Insert|List"
    ContextTypeName="string"
    DataField="string"
    TableName="string"
    OnDataBinding="DataBinding event handler"
    OnPreRender="PreRender event handler"/>

Remarks

The DynamicHyperLink control enables you to create the following types of links:

  • Table-row data-bound links, which define action links that apply to table rows, such as "Edit" or "Details". Dynamic Data infers the table and the primary key from the row itself.

  • Links to MetaTable objects, which in turn point to objects such as tables.

  • Unbound links, which specify actions outside a data-bound control. In this case, you do not specify an Action property value. Instead, you define the table to access and the action to perform.

Example

The following examples show how to create DynamicHyperLink controls for data rows and for a collection, and to perform a custom action.

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="DynamicDataDynamicHyperlink.aspx.vb" Inherits="DocSamples_DynamicDataDynamicHyperlink" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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"   
            ContextTypeName="AdventureWorksLTDataContext"
            TableName="ProductModels">Insert new item
        </asp:DynamicHyperLink>

        <br />

        <asp:DynamicHyperLink ID="DynamicHyperLink2" runat="server" 
            Action="Edit" 
            ContextTypeName="AdventureWorksLTDataContext"
            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">
                            <%# 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>



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

    </div>
    </form>
</body>
</html>
<%@ 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>



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

    </div>
    </form>
</body>
</html>
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.DynamicData

Partial Public Class DocSamples_DynamicDataDynamicHyperlink
    Inherits System.Web.UI.Page

    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.model.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.
            GridView2.DataSource = tables
            GridView2.DataBind()
        End If
    End Sub

    Protected Sub DynamicHyperLink_DataBinding( _
    ByVal sender As Object,ByVal e As EventArgs)
        Dim table As MetaTable = DirectCast(GetDataItem(), MetaTable)
        Dim dynamicHyperLink As DynamicHyperLink = _
        DirectCast(sender, DynamicHyperLink)
        ' Set the context type name for the DynamicHyperLink.
        dynamicHyperLink.ContextTypeName = _
        table.DataContextType.AssemblyQualifiedName
    End Sub
End Class
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;
    }
}

See Also

Tasks

How to: Create Table Action Links in Dynamic Data

Reference

DynamicHyperLink