In ASP.Net you can attach the
onclick event to Hyperlink control in two ways. First way is to add and assign the value at the time of designing by adding the
onclick property as an attribute of
HyperLink control in the HTML markup code block.
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl="Default2.aspx"
onclick="return confirm('Are you sure?');">
Hyperlink
</asp:HyperLink>
The Second way is to add the onclick attribute programmatically at the server side.
HyperLink1.Attributes.Add("onclick", "return confirm('Are you sure to navigate to other page?');");
In the example below a javascript function will show a confirm message with Ok and cancel button. If the user will click the cancel button then it will return false otherwise true.
<script type="text/javascript">
function confirmNavigation(){
return confirm('Are you sure to navigate to other page?');
}
</script>
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl="Default2.aspx"
onclick="return confirmNavigation();">
Hyperlink
</asp:HyperLink>