Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make (link)button function as hyperlink?

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.

like image 764
user763554 Avatar asked Dec 26 '12 00:12

user763554


People also ask

How do you make a button a hyperlink?

The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute. If necessary, set CSS display: inline; on the form to keep it in the flow with the surrounding text. Instead of <input type="submit"> in above example, you can also use <button type="submit"> .

Can a button have an href?

HTML buttons cannot have href attribute if they are created using button <button> </button> HTML tags. However, you can use href attribute if you create the buttons using link <a> </a> HTML tags.

How do I link a button to another page in HTML?

To create a button link to another page in HTML,just add <a> tag and wrap it around the simple Html button. Inside a <a> tag simply use href=“” attribute to give the path of the desired page.

How do I make a button go to another page?

how to make a button or a page link to another page in HTML using the button. Just write/Declare your HTML Button inside HTML Anchor tags <a>. Anchor tags will make our HTML Buttons Clickable and after that, you can use Anchor tag's href attribute to give the Path to your Button.


2 Answers

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;
}
like image 51
Nils Anders Avatar answered Sep 22 '22 21:09

Nils Anders


There is a middle way. If you want a HTML control but you need to access it server side you can simply add the runat="server" attribute:

<a runat="server" Id="lnkBack">Back</a>

You can then alter the href server side using Attributes

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       lnkBack.Attributes.Add("href", url);
    }
}

resulting in:

<a id="ctl00_ctl00_mainContentPlaceHolder_contentPlaceHolder_lnkBack" 
      href="url.aspx">Back</a>
like image 39
Liam Avatar answered Sep 20 '22 21:09

Liam