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

发布于 2024-07-12 04:51:42 字数 238 浏览 7 评论 0原文

我想使用 asp:LinkBut​​ton,因为它看起来像一个链接,但也有服务器端单击处理程序。

但网络服务器似乎无法检测客户端是否禁用了 javascript,并且不会呈现为仍然有效的机制。

是否可以有一个看起来像链接但具有服务器端 OnClick 事件处理程序的链接?


答案

答案是否定的,但以下是一些解决方法。 接受非零赞成票的一项。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如痴如狂 2024-07-19 04:51:42

您可以使用 CSS 将按钮设计为看起来像链接,但这在很大程度上取决于浏览器,具体取决于 CSS 实现。


编辑:我觉得有必要完成我的答案,因为它已被接受。

asp:LinkBut​​ton 呈现为 HTML 链接,因此无法发布到网页; 但只能发出 get 请求。 要解决此问题,请使用 JavaScript 来操作帖子。 然而,如果 Javascript 被禁用,这是不可能的。

asp:Buttonasp:ImageButton 是不同的。 他们通过使用真正的 HTML 表单元素发布到网页(或根据表单属性获取)来提交 HTML 表单。 所以这些都可以在没有 JS 干预的情况下工作。

某些浏览器允许 CSS 样式将按钮设置为看起来像链接,因此这可以用来解决该问题。

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.

明明#如月 2024-07-19 04:51:42

只是一个想法:

渲染一个输入按钮并使用 javascript 将其更改为链接。 该按钮适用于不支持 JavaScript 的浏览器,并成为那些支持 JavaScript 的浏览器的链接。

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.

小猫一只 2024-07-19 04:51:42

对于 ASP.Net 中的此类内容,我通常会呈现控件以及用 JavaScript 隐藏的“可访问控件”。 因此,在这种情况下,它将呈现一个 LinkBut​​ton(默认情况下通过样式隐藏)和一个普通按钮,并注册一些 javascript 来隐藏 Button 并显示 LinkBut​​ton。

对于 ASP.Net 控件来说,这是一种非常常见的解决方法,如果没有 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. 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.

倚栏听风 2024-07-19 04:51:42

没有看起来像链接的 INPUT 类型。 如果您想使用链接在网页上执行输入类型活动,则必须导航到您所在的同一页面。

您可以通过链接传递查询字符串变量,并对其做出响应。 它的行为不会完全像回发,而是只会导航到您所在的同一页面。

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.

◇流星雨 2024-07-19 04:51:42

仅供记录,您所要求的是可能的。

您必须像这样配置控件:

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

并将其放在代码隐藏中:

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

您可以找到更详细的信息此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文