Manage Hyperlinks in Worksheet
Working with Hyperlinks
Types of Hyperlinks
Generally,the following hyperlinks are supported by Aspose.Cells.GridWeb:
- URL hyperlinks, hyperlinks that can be linked to web URLs.
- Text hyperlinks, URL hyperlinks applied to text.
- Image hyperlinks, URL hyperlinks applied to images.
- Cell command hyperlinks, hyperlinks that post data to a server. Such hyperlinks act more like a button that triggers a server-side event when clicked.
The below sections describe the use of all types of hyperlinks in detail. It also discusses how to access or remove links.
Adding Hyperlinks
URL Hyperlinks
URL hyperlinks look more like simple hyperlinks that you normally see on websites. A URL hyperlink works like an anchor in a cell. Whenever it is clicked, it navigates to a web page or opens a new browser window.
There are different types of URL hyperlinks:
- Text hyperlinks.
- Image hyperlinks.
Developers can specify an image for the hyperlink. If an image isn’t specified, a text hyperlink is created; else an image hyperlink is created.
Text Hyperlinks
To add a text hyperlink to a worksheet:
- Add the Aspose.Cells.GridWeb control to your Web Form.
- Access a worksheet.
- Add a hyperlink to a cell in the worksheet.
- Set the text that will be shown in the cell.
- Set the hyperlink’s URL.
- Set the hyperlink’s target,if desired.
- Set a tool tip, if desired.
The example below adds two hyperlinks to a worksheet. One has no target while the other is set to _parent.
Output: text hyperlinks added to worksheet
Image Hyperlinks
To add an image hyperlink:
- Add the Aspose.Cells.GridWeb control to your Web Form.
- Access a worksheet.
- Add a hyperlink to a cell.
- Set the URL of the image that will be displayed as hyperlink.
- Set the hyperlink URL.
- Set a tool tip, if desired.
- Set the hyperlink text, if desired.
Output: image hyperlinks added to worksheet
Setting the image hyperlink’s AltText fills a similar function as setting an
The image for the image URL could not be found
Cell Command Hyperlinks
A cell command hyperlink is a special type of hyperlink which triggers a server-side event instead of opening a web page. Developers can add code to the server-side event and perform any task when the hyperlink is clicked. This feature enables developers to create more interactive applications.
To add a cell command hyperlink:
- Add the Aspose.Cells.GridWeb control to your Web Form.
- Access a worksheet.
- Add a hyperlink to a cell.
- Set the hyperlink’s Command to any desired value. The value is used by the hyperlink’s event handler to recognise it.
- Set a tool tip, if desired.
- Set the URL for the Image that will be displayed as a hyperlink.
A cell command hyperlink has been added to worksheet
Event Handling of Cell Command Hyperlinks
Developers need to create an event handler for the GridWeb control’s CellCommand event to perform specific tasks when a specific cell command hyperlink is clicked. The CellCommand event’s event handler provides an object of the CellEventArgs type that offers the Argument property. Use the Argument property to identify a specific hyperlink by comparing its CellCommand value.
The example below creates an event handler for the cell command hyperlink created in the code above. The hyperlink’s CellCommand was set to Click. So, in the event handler, first check it and then add code which displays a message in the A6 cell.
The event handler is invoked when the hyperlink is clicked.
Output: text added to A6 cell when hyperlink is clicked
Accessing Hyperlinks
To access an existing hyperlink:
- Access the cell that contains it.
- Get the cell reference.
- Pass the reference to the Hyperlinks collection’s GetHyperlink method to access the hyperlink.
- Modify the hyperlink’s properties.
Removing Hyperlinks
To remove a hyperlink:
- Access the active worksheet.
- Remove a hyperlink using the Hyperlinks collection’s Remove method.