简体   繁体   English

ASP.NET linkbutton enabled = false在Firefox中不起作用

[英]ASP.NET linkbutton enabled=false does not work in Firefox

I want to disable an ASP.NET linkbutton . 我想禁用ASP.NET linkbutton What I want is a solution where browsers won't let me click on the linkbutton or at least where the associated action is not executed. 我想要的是一个解决方案,其中浏览器不允许我单击链接按钮,或者至少不执行相关操作。

I do the following on the HTML markup using WebForms : 我使用WebForms在HTML标记上执行以下操作:

 <asp:LinkButton
       id="link1"
       text="Some button"
       runat="server"
       Enabled ="false"
 />

I trigger the link button action using jQuery events: 我使用jQuery事件触发链接按钮操作:

$('#link1').click(function (evt) {
    // Do something here
    return false;
});
  • In IE8 it is disabled and does not allow click 在IE8中,它被禁用并且不允许点击
  • In Chrome it's not disabled but id does not allow click 在Chrome中未禁用它,但ID不允许点击
  • In Firefox 41.0.1 I can still click the link button and perform the action 在Firefox 41.0.1中,我仍然可以单击链接按钮并执行操作

The result is the same if I disable it in code behind: 如果在后面的代码中将其禁用,结果将相同:

this.link1.Enabled = false;

It's not clear whether it's been missed in your simplification but it seems that your jQuery would not be executed due to a change in the button's client side ID. 不清楚您是否简化了它,但是由于按钮的客户端ID的更改,看来您的jQuery无法执行。 You will need to do either of the following: 您将需要执行以下任一操作:

Set ClientIDMode to static on your control ... 在控件上将ClientIDMode设置为static ...

To ensure that the id remains as link1 for use in your client side code. 确保id保留为link1以便在客户端代码中使用。

<asp:LinkButton
    id="link1"
    text="Some button"
    runat="server"
    Enabled ="false"
    ClientIDMode="static"
    />

or, obtaining the ID of the your control in your jQuery at runtime ... 或者,在运行时在jQuery中获取控件的ID ...

Although this will only work if your jQuery is within the same web form page, not external JS. 尽管这仅在您的jQuery位于同一Web表单页面而不是外部JS内有效。

$('#<%= link1.ClientID %>').click(function (evt) {
    // Do something here
    return false;
});

Whichever you choose ... 无论您选择...

If it correctly linked, you can then use jQuery's preventDefault() method to disable the post back from triggering. 如果链接正确,则可以使用jQuery的preventDefault()方法禁用回发触发。

$('#link1').click(function (evt) {
    evt.preventDefault();
    // Do something here
});

Making a Linkbutton disabled does not prevent it from being clicked. 禁用链接按钮不会阻止其被单击。 In my test a disabled linkbutton rendered like this: 在我的测试中,禁用的linkbutton呈现如下:

<a class="aspNetDisabled">test</a>

jQuery will happily attach a click event to that anchor tag. jQuery将愉快地将click事件附加到该锚标记。 You need to either prevent the event from triggering, or change the code so that it only performs your actions when the button is enabled. 您需要防止事件触发或更改代码,以便仅在启用按钮后才执行您的操作。

Here's a possible work-around which will only execute your js if the button is enabled: 这是一个可能的解决方法,仅在启用该按钮后才会执行您的js:

$('#link1').click(function (evt) {
    if('<%=link1.Enabled%>' === 'True') {
      // Do something here
    }
    return false;
});

Even if the client triggers the disabled button, the if statement will cause Do something here to not run. 即使客户端触发了禁用按钮,if语句也会导致Do something here执行操作”无法运行。

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

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