简体   繁体   English

ASP.NET:asp:禁用Javascript的LinkBut​​ton?

[英]ASP.NET: asp:LinkButton with Javascript disabled?

i want to use an asp:LinkButton , since it looks like a link, yet also has server-side Click handler. 我想使用asp:LinkButton ,因为它看起来像一个链接,但也有服务器端点击处理程序。

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. 但是,Web服务器似乎无法检测到客户端上是否禁用了javascript,并且无法呈现为仍然有效的机制。

Is it possible to have a link that looks like a link, but has server-side OnClick event handler? 是否有可能有一个看起来像链接,但有服务器端OnClick事件处理程序的链接?


Answer 回答

The answer is no, but below are some workaround ideas. 答案是否定的,但下面是一些解决方法的想法。 Accepted the one with non-zero up-votes. 接受了非零向上投票的那个。

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. 您可以使用CSS将按钮设置为看起来像链接,但这将取决于CSS实现,这将取决于浏览器。


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; asp:LinkButton呈现为HTML链接,因此无法发布到网页; but can only make get requests. 但只能使GET请求。 To work around this MS use JavaScript to action the post. 要解决此问题,请使用JavaScript来操作帖子。 This is not possible however if Javascript is disabled. 但是,如果禁用Javascript,则无法执行此操作。

asp:Button and asp:ImageButton are different. asp:Buttonasp:ImageButton是不同的。 They submit the HTML form by posting to a web page (or get depending on the form attributes) by using true HTML form elements. 他们通过使用真正的HTML表单元素发布到网页(或取决于表单属性)来提交HTML表单。 So these will work without JS intervention. 所以这些都可以在没有JS干预的情

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. 有些浏览器会允许CSS样式将按钮设置为看起来像链接,因此这可以用来解决问题。

Just an idea: 只是一个想法:

Render an input button and use javascript to change it into a link. 渲染输入按钮并使用javascript将其更改为链接。 The button would work for non-javascript enabled browser and become a link for those who have javascript. 该按钮适用于非启用javascript的浏览器,并成为拥有javascript的用户的链接。

With anything like this in ASP.Net I'd usually render the control, along with an "accessible control" that's hidden with JavaScript. 在ASP.Net中有这样的东西,我通常会渲染控件,以及一个用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. 因此,在这种情况下,它将呈现一个LinkBut​​ton(默认情况下通过样式隐藏)和一个普通按钮,注册了一些javascript以隐藏Button并显示LinkBut​​ton。

It's quite a common workaround for ASP.Net control that don't play nicely without javascript, or when you need "Autopostback" without Javascript. 对于ASP.Net控件而言,这是一个非常常见的解决方法,如果没有javascript,或者在没有Javascript的情况下需要“Autopostback”时效果不佳。

There is no INPUT type which will look like a link. 没有INPUT类型看起来像链接。 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. 如果要使用链接在网页上执行INPUT类型活动,则必须导航到您所在的同一页面。

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. 它不会完全像回发一样,而只会导航到您所在的同一页面。

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 . 您可以在此处找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM