ASP.NET: asp:LinkButton with Javascript disabled?

我的梦境 提交于 2020-01-14 07:59:05

问题


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.


回答1:


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.




回答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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/480498/asp-net-asplinkbutton-with-javascript-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!