ComponentOne ReportViewer for ASP.NET Web Forms
Task-Based Help / Adding Hyperlinksto Report Documents
In This Topic
    Adding Hyperlinksto Report Documents
    In This Topic

    The C1ReportViewer control allows you to include hyperlinks. You can add links that allow users to open Web pages or other reports, jump to another location within the same report, change C1ReportViewer appearance, or execute custom JavaScript.

    Adding a Link to the Middle of the Document

    Add the following code to the Page_Load event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' to jump to a render object, an anchor is really not needed:
    Dim rt4 As New RenderText("Click here to go to the middle of document.")
    rt4.Hyperlink = New C1Hyperlink(doc.Body.Children(doc.Body.Children.Count / 2))
    rt4.Hyperlink.StatusText = "Go to the approximate middle of the document"
    doc.Body.Children.Add(rt4)
    

    To write code in C#

    C#
    Copy Code
    // to jump to a render object, an anchor is really not needed:
    RenderText rt4 = new RenderText("Click here to go to the middle of document.");
    rt4.Hyperlink = new C1Hyperlink(doc.Body.Children[doc.Body.Children.Count / 2]);
    rt4.Hyperlink.StatusText = "Go to the approximate middle of the document";
    doc.Body.Children.Add(rt4);
    

    This code above adds a link to the middle of the document.

    Adding a Hyperlink to an Anchor

    Add the following code to the Page_Load event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' make an anchor
    Dim rt1 As New RenderText("This is text with anchor1.")
    ' the name ("anchor1") will be used to jump to this link:
    rt1.Anchors.Add(New C1Anchor("anchor1"))
    rt1.Hyperlink = New C1Hyperlink(New C1LinkTargetPage(PageJumpTypeEnum.Last), "Go to the last page of the document")
    doc.Body.Children.Add(rt1)
     
    ' add hyperlink to anchor1
    Dim rt2 As New RenderText("Click here to go to anchor1.")
    rt2.Hyperlink = New C1Hyperlink(New C1LinkTargetAnchor("anchor1"), "This is status text when the mouse hovers over link to anchor1")
    doc.Body.Children.Add(rt2)
    

    To write code in C#

    C#
    Copy Code
    // make an anchor
    RenderText rt1 = new RenderText("This is text with anchor1.");
    // the name ("anchor1") will be used to jump to this link:
    rt1.Anchors.Add(new C1Anchor("anchor1"));
    rt1.Hyperlink = new C1Hyperlink(new C1LinkTargetPage(PageJumpTypeEnum.Last), "Go to the last page of the document");
    doc.Body.Children.Add(rt1);
     
    // add hyperlink to anchor1
    RenderText rt2 = new RenderText("Click here to go to anchor1.");
    rt2.Hyperlink = new C1Hyperlink(new C1LinkTargetAnchor("anchor1"),
           "This is status text when the mouse hovers over link to anchor1");
    doc.Body.Children.Add(rt2);
    

    This code above adds an image that is linked to an external site.

    Executing a Custom JavaScript Action

    Add the following code to the Page_Load event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim rt5 As New RenderText("alert 'Hello'.")
    rt5.Hyperlink = New C1Hyperlink(New C1LinkTargetFile("javascript:alert('Hello')"))
    rt5.Hyperlink.StatusText = "Show 'Hello' message."
    doc.Body.Children.Add(rt5)
    

    To write code in C#

    C#
    Copy Code
    RenderText rt5 = new RenderText("alert 'Hello'.");
    rt5.Hyperlink = new C1Hyperlink(new C1LinkTargetFile("javascript:alert('Hello')"));
    rt5.Hyperlink.StatusText = "Show 'Hello' message.";
    doc.Body.Children.Add(rt5);
    

    This code above adds a link to an alert dialog box.

    Opening a Print Preview Dialog Box

    Add the following code to the Page_Load event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim rt6 As New RenderText("printWithPreview")
    rt6.Hyperlink = New C1Hyperlink(New C1LinkTargetFile("exec:printWithPreview()"))
    rt6.Hyperlink.StatusText = "Preview and print report."
    doc.Body.Children.Add(rt6)
    

    To write code in C#

    C#
    Copy Code
    RenderText rt6 = new RenderText("printWithPreview");
    rt6.Hyperlink = new C1Hyperlink(new C1LinkTargetFile("exec:printWithPreview()"));
    rt6.Hyperlink.StatusText = "Preview and print report.";
    doc.Body.Children.Add(rt6);
    

    This code above adds a link to a print preview dialog box.

    Opening Hyperlinks in a New Window

    By default, when clicked links set with the LinkTarget property of C1ReportViewer open in the same browser window. However, you may choose to have links open in a new window. In order to override the default behavior, you'll need to write custom script for the hyperlinks included in your report. These hyperlinks can be created explicitly and can be added in the body of the report's document.

    For example, add the following code:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim rt2 As New RenderText("execute custom script.")
    rt2.Hyperlink = New C1Hyperlink(New C1LinkTargetFile("javascript:openPage('http://google.com/')"))
    doc.Body.Children.Add(rt2)
    

    To write code in C#

    C#
    Copy Code
    RenderText rt2 = new RenderText("execute custom script.");
    rt2.Hyperlink = new C1Hyperlink(new C1LinkTargetFile("javascript:openPage('http://google.com/')"));
    doc.Body.Children.Add(rt2);
    

    The above code uses a method called openPage which is defined in JavaScript as follows:

    To write code in Source View

    <script type="text/javascript">
    function openPage(url){
    window.open(url, "mywindow");
    }
    </script>

    This method tells the C1ReportViewer that the target should open in a new browser window. Instead of creating new hyperlink, you can also directly set the LinkTarget property to the desired link with the code below:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Protected Function MakeDoc() As C1PrintDocument
        Dim doc As New C1PrintDocument()
        Dim report As C1Report = C1ReportViewer.CreateC1Report()
     
        report.Load(Server.MapPath("~/XML/CommonTasks.xml"), "01: Alternating Background (Greenbar report)")
        report.Render()
     
        report.Sections("Detail").Fields("QuantityPerUnitCtl").LinkTarget = "javascript:openPage('http://google.com/')"
        doc = report.C1Document
        Return doc
    End Function
    

    To write code in C#

    C#
    Copy Code
    protected C1PrintDocument MakeDoc()
    {
       C1PrintDocument doc = new C1PrintDocument();
       C1Report report = C1ReportViewer.CreateC1Report();
     
       report.Load(Server.MapPath("~/XML/CommonTasks.xml"), "01: Alternating Background (Greenbar report)");
       report.Render();
     
       report.Sections["Detail"].Fields["QuantityPerUnitCtl"].LinkTarget = "javascript:openPage('http://google.com/')";
       doc = report.C1Document;
       return doc;
    }
    

    This script initializes the resizing links.