Introduction
In this topic, we would discuss in detail that what types of hyperlinks are supported in Aspose.Grid.Web and how can we manage them programmatically. Hyperlinks in Aspose.Grid.Web can be used for either creating links to web URLs or performing postback to server.
Types of Hyperlinks
Generally, there are two types of hyperlinks supported by Aspose.Grid.Web, which include the following:
- URL Hyperlinks, hyperlinks that can be linked with some web URLs
- Text Hyperlinks, represent those URL hyperlinks that can be applied on text
- Image Hyperlinks, represent those URL hyperlinks that can be applied on images
- Cell Command Hyperlinks, hyperlinks that post data to server. Such hyperlinks act more like a button that triggers a server side event, when clicked
In the below sections, we will describe the use of all types of hyperlinks in detail. Moreover, it will also be discussed that how can we access or remove them.
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 may navigate to another web page or open a new browser window depending upon user requirements.
There are further two types of URL Hyperlinks as follows:
- Text Hyperlinks
- Image Hyperlinks
Developers can specify an image for the hyperlink. So, if developers don't specify an image for the hyperlink then a Text Hyperlink is created otherwise an Image Hyperlink is added to worksheet.
Text Hyperlinks
To add a Text Hyperlink to your worksheet, please follow the steps below:
- Add Aspose.Grid.Web control to your Web Form
- Access the worksheet in which it is required to add hyperlinks
- Add Hyperlink to some specific cell of the worksheet
- Set Text of the Hyperlink that will be shown in cell
- Set URL of the Hyperlink
- Set Target of the Hyperlink,if desired
- Set Tool Tip of the Hyperlink, if desired
NOTE: Hyperlink target can be set to _self, _top or _parent for opening web URLs in New, Current or Top windows respectively.
In the example given below, we have added two hyperlinks in a worksheet. One hyperlink has no target while other hyperlink is set to _parent target.
Example:
[C#]
//Accessing the reference of the worksheet that is currently active
WebWorksheet sheet=GridWeb1.WebWorksheets[GridWeb1.ActiveSheetIndex];
//Adding hyperlink to the worksheet
Hyperlink link1 = sheet.Hyperlinks.AddHyperlink("B1");
//Setting text of the hyperlink
link1.Text = "Aspose";
//Setting URL of the hyperlink
link1.Url = "http://www.aspose.com";
//Setting tool tip of the hyperlink
link1.ToolTip = "Open Aspose Web Site in new window";
//Adding hyperlink to the worksheet
Hyperlink link2 = sheet.Hyperlinks.AddHyperlink("B2");
//Setting text of the hyperlink
link2.Text = "Aspose.Grid Technical Support";
//Setting URL of the hyperlink
link2.Url = "http://www.aspose.com/Community/Forums/258/ShowForum.aspx";
//Setting target of the hyperlink
link2.Target = "_parent";
//Setting tool tip of the hyperlink
link2.ToolTip = "Open Aspose.Grid Technical Support in parent window";
[VB.NET]
'Accessing the reference of the worksheet that is currently active
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(GridWeb1.ActiveSheetIndex)
'Adding hyperlink to the worksheet
Dim link1 As Hyperlink = sheet.Hyperlinks.AddHyperlink("B1")
'Setting text of the hyperlink
link1.Text = "Aspose"
'Setting URL of the hyperlink
link1.Url = "http://www.aspose.com"
'Setting tool tip of the hyperlink
link1.ToolTip = "Open Aspose Web Site in new window"
'Adding hyperlink to the worksheet
Dim link2 As Hyperlink = sheet.Hyperlinks.AddHyperlink("B2")
'Setting text of the hyperlink
link2.Text = "Aspose.Grid Technical Support"
'Setting URL of the hyperlink
link2.Url = "http://www.aspose.com/Community/Forums/258/ShowForum.aspx"
'Setting target of the hyperlink
link2.Target = "_parent"
'Setting tool tip of the hyperlink
link2.ToolTip = "Open Aspose.Grid Technical Support in parent window"
The output of above code snippet is displayed in the figure shown below:
|
Figure: Text Hyperlinks added to worksheet
|
Image Hyperlinks
To add an Image Hyperlink to your worksheet, please follow the steps below:
- Add Aspose.Grid.Web control to your Web Form
- Access the worksheet in which it is required to add hyperlinks
- Add Hyperlink to some specific cell of the worksheet
- Set URL of the Image that will be displayed as Hyperlink
- Set URL of the Hyperlink
- Set Tool Tip of the Hyperlink, if desired
- Set Text of the Hyperlink, if desired
Example:
[C#]
//Accessing the reference of the worksheet that is currently active
WebWorksheet sheet=GridWeb1.WebWorksheets[GridWeb1.ActiveSheetIndex];
//Adding hyperlink to the worksheet
Hyperlink link1 = sheet.Hyperlinks.AddHyperlink("B1");
//Setting URL of the image that will be displayed as hyperlink
link1.ImageUrl = "style/Aspose.Banner.gif
//Setting URL of the hyperlink
link1.Url = "http://www.aspose.com";
//Setting tool tip of the hyperlink
link1.ToolTip = "Open Aspose Web Site in new window";
//Adding hyperlink to the worksheet
Hyperlink link2 = sheet.Hyperlinks.AddHyperlink("B2");
//Setting URL of the image that will be displayed as hyperlink
link2.ImageUrl = "../style/Aspose.Grid.gif";
//Setting URL of the hyperlink
link2.Url = "http://www.aspose.com/Products/Aspose.Grid/Default.aspx";
//Setting tool tip of the hyperlink
link2.ToolTip = "Open Aspose.Grid Technical Support in parent window";
//Setting text of the hyperlink. It will be displayed if image is not displayed due to some reason
link2.Text = "Open Aspose.Grid Page in new window";
[VB.NET]
'Accessing the reference of the worksheet that is currently active
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(GridWeb1.ActiveSheetIndex)
'Adding hyperlink to the worksheet
Dim link1 As Hyperlink = sheet.Hyperlinks.AddHyperlink("B1")
'Setting URL of the image that will be displayed as hyperlink
link1.ImageUrl = "style/Aspose.Banner.gif
'Setting URL of the hyperlink
link1.Url = "http://www.aspose.com"
'Setting tool tip of the hyperlink
link1.ToolTip = "Open Aspose Web Site in new window"
'Adding hyperlink to the worksheet
Dim link2 As Hyperlink = sheet.Hyperlinks.AddHyperlink("B2")
'Setting URL of the image that will be displayed as hyperlink
link2.ImageUrl = "../style/Aspose.Grid.gif"
'Setting URL of the hyperlink
link2.Url = "http://www.aspose.com/Products/Aspose.Grid/Default.aspx"
'Setting tool tip of the hyperlink
link2.ToolTip = "Open Aspose.Grid Technical Support in parent window"
'Setting text of the hyperlink. It will be displayed if image is not displayed due to some reason
link2.Text = "Open Aspose.Grid Page in new window"
The output of above code snippet is displayed in the figure shown below:
|
Figure: Image Hyperlinks added to worksheet
|
NOTE: Setting Text of the Image Hyperlink performs a different function. This text will be displayed only when hyperlinked image is not displayed due to some reason. The most common reason could be the unavailibility of image at the specified image URL. In case, if the image of second hyperlink is not found then the output of above code snippet would be as shown below:
|
Figure: Image Hyperlink view, if image is not found
|
Cell Command Hyperlinks
A Cell Command Hyperlink is a special kind of hyperlink that triggers a server side event instead of opening a web URL. Developers can add their desired code to this server side event and perform any desired task when the hyperlink is clicked. This feature enables developers to create more interactive applications.
To add a Cell Command Hyperlink to your worksheet, please follow the steps below:
- Add Aspose.Grid.Web control to your Web Form
- Access the worksheet in which it is required to add hyperlinks
- Add Hyperlink to some specific cell of the worksheet
- Set Action Type of the Hyperlink to CellCommand
- Set the Cell Command of hyperlink to any desired value. This value will be used in the Event Handler of this Hyperlink to recognize it
- Set Tool Tip of the Hyperlink, if desired
- Set URL of the Image that will be displayed as Hyperlink
Example:
[C#]
//Accessing the reference of the worksheet that is currently active
WebWorksheet sheet=GridWeb1.WebWorksheets[GridWeb1.ActiveSheetIndex];
//Adding hyperlink to the worksheet
Hyperlink link1 = sheet.Hyperlinks.AddHyperlink("A2");
//Setting the action type of the link to CellCommand
link1.ActionType = HyperlinkActionType.CellCommand;
//Setting the cell command for the link
link1.CellCommand = "Click";
//Setting tool tip of the hyperlink
link1.ToolTip = "Click Here";
//Setting URL of the button image that will be displayed as hyperlink
link1.ImageUrl = "style/button1.bmp"
[VB.NET]
'Accessing the reference of the worksheet that is currently active
Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(GridWeb1.ActiveSheetIndex)
'Adding hyperlink to the worksheet
Dim link1 As Hyperlink = sheet.Hyperlinks.AddHyperlink("A2")
'Setting the action type of the link to CellCommand
link1.ActionType = HyperlinkActionType.CellCommand
'Setting the cell command for the link
link1.CellCommand = "Click"
'Setting tool tip of the hyperlink
link1.ToolTip = "Click Here"
'Setting URL of the button image that will be displayed as hyperlink
link1.ImageUrl = "style/button1.bmp"
The output of above code snippet is displayed in the figure shown below: