How to make (link)button function as hyperlink?

user763554 picture user763554 · Dec 26, 2012 · Viewed 165.9k times · Source

How do I use an asp:Button or asp:LinkButton as asp:Hyperlink?

The existing Hyperlink just goes to another section on the same page: NavigateUrl="#Section2"

I want to do this in the aspx file without additional coding. Thanks.

The purpose is to have a button look instead of the underlined text BUT I don't want to use an image with hyperlink to achieve this purpose.

Answer

Nils Anders picture Nils Anders · Dec 26, 2012

You can use OnClientClick event to call a JavaScript function:

<asp:Button ID="Button1" runat="server" Text="Button" onclientclick='redirect()' />

JavaScript code:

function redirect() {
  location.href = 'page.aspx';
}

But i think the best would be to style a hyperlink with css.

Example :

.button {
  display: block;
  height: 25px;
  background: #f1f1f1;
  padding: 10px;
  text-align: center;
  border-radius: 5px;
  border: 1px solid #e1e1e2;
  color: #000;
  font-weight: bold;
}