7

i want to use an asp:LinkButton, since it looks like a link, yet also has server-side Click handler.

But the web-server seems unable to detect if javascript is disabled on the client, and doesn't render into a mechanism that still works.

Is it possible to have a link that looks like a link, but has server-side OnClick event handler?


Answer

The answer is no, but below are some workaround ideas. Accepted the one with non-zero up-votes.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

5 Answers5

7

You could use CSS to style a button to look like a link, but this will be very much browser dependant, depending on the CSS implementation.


Edit: I feel compelled to complete my answer since it's been accepted.

An asp:LinkButton renders to an HTML link, and as such cannot post to a web page; but can only make get requests. To work around this MS use JavaScript to action the post. This is not possible however if Javascript is disabled.

asp:Button and asp:ImageButton are different. They submit the HTML form by posting to a web page (or get depending on the form attributes) by using true HTML form elements. So these will work without JS intervention.

Some browsers will allow CSS styling to style a button to look like a link, and as such this can be used to work around the issue.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Ady
  • 4,736
  • 2
  • 22
  • 20
  • 3
    A more complete version would have some sample code - otherwise it just begs another SO question called "How do i use CSS and Javascript" to create a button that looks like a link. – Ian Boyd Jan 27 '09 at 19:15
2

Just an idea:

Render an input button and use javascript to change it into a link. The button would work for non-javascript enabled browser and become a link for those who have javascript.

Cristian Libardo
  • 9,260
  • 3
  • 35
  • 41
2

With anything like this in ASP.Net I'd usually render the control, along with an "accessible control" that's hidden with JavaScript. So in this case it would render a LinkButton (hidden by default via styles), and a normal button, with some javascript registered to hide the Button and show the LinkButton.

It's quite a common workaround for ASP.Net control that don't play nicely without javascript, or when you need "Autopostback" without Javascript.

Steven Robbins
  • 26,441
  • 7
  • 76
  • 90
0

There is no INPUT type which will look like a link. If you want to use a link to perform an INPUT type activity on a web page, it will have to navigate to the same page that you are on.

You can pass a query string variable with the link, and respond to that. It won't act exactly like a postback, instead it will just navigate to the same page that you are on.

Matt Brunell
  • 10,141
  • 3
  • 34
  • 46
0

Just for the record, what you're asking for is possible.

You have to to configure the control like this:

<asp:LinkButton ID="lbtnRequestReview" Text="[ Request Plan Review ]" OnCommand="lbtnRequestReview_Command" CommandName="cmd" CommandArgument="0" CausesValidation="false" runat="server"/>

and put this in the code-behind:

protected void lbtnRequestReview_Command(Object sender, CommandEventArgs e)
        {
           //...
        }

You can find more detailed information here.

draconis
  • 599
  • 1
  • 5
  • 16