C# 禁用 LinkButton 不会禁用 JavaScript 中的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/754497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling LinkButton doesn't disable the click event in javascript
提问by Ben
I want to disable a LinkButton
clink on the client site.
我想禁用LinkButton
客户端站点上的叮当声。
objLinkButton.disabled = true;
// or
objLinkButton.disabled = -1;
This disables the link but I am still able to click on the link and do PostBack.
这会禁用链接,但我仍然可以单击链接并执行回发。
Is there any way I can disable the link.
有什么办法可以禁用链接。
Code:
代码:
<asp:linkbutton id="xyz" runat="server"
onClick="javascript:LinkDisable(this)" ></asp:linkbutton>
which renders as a link which does a postback... I am opening the page on postback in a new window. What I want to do is.. when I click on the link for the firsttime.. it will open a new page and then it will disable the link.
它呈现为执行回发的链接...我在新窗口中打开回发页面。我想要做的是..当我第一次点击链接时..它会打开一个新页面,然后它会禁用该链接。
what I am doing is .. onClick of that link I have a javascript function.. which is something like this..
我正在做的是.. onClick 该链接我有一个 javascript 函数.. 是这样的..
In LinkDisable ...
在链接禁用...
function LinkDisable(obj)
{
obj.disabled = -1;
obj.href = '#';
//Cant return false from here.. otherwise it wont postback...
}
When I do this.. the link gets grey's out ... but I am still able to click it. I want to stop the user from clicking it the second time.
当我这样做时..链接变灰了......但我仍然可以点击它。我想阻止用户第二次点击它。
Any help is appreciated.
任何帮助表示赞赏。
采纳答案by Ben
Found the Solution guys... these javascript solution will work but if you refresh the page... it will clear the varaibles.
找到解决方案的家伙...这些 javascript 解决方案将起作用,但如果您刷新页面...它将清除变量。
So thats a bug right there..
所以那是一个错误..
Found the solution using "userData" ... IE Session data... it really cool.
使用“userData”找到了解决方案...... IE 会话数据......真的很酷。
Checkout this link:http://www.eggheadcafe.com/articles/20010615.asp
回答by DLS
This is an edit based on the comment you posted to allow the linkbutton to be followed through on the first click but not otherwise.
这是基于您发布的评论进行的编辑,以允许在第一次点击时跟踪链接按钮,否则不会。
To allow the link to be clicked the first time, but not after that, create a variable on the page to keep track of the click.
要允许第一次点击链接,但之后不能点击,请在页面上创建一个变量来跟踪点击。
var clicked = 0;
Since linkbuttons produce hrefs on the front end, you could do this
由于链接按钮在前端产生 hrefs,你可以这样做
<a href="#" OnClientClick="return false"></a>
OnClientClick is specific to .NET
OnClientClick 特定于 .NET
If you want to do it after the page loads in certain situations only:
如果您只想在某些情况下在页面加载后执行此操作:
<a href="" id="linkbutton">
var linkbutton = document.getElementById("linkbutton");
linkbutton.onclick = function(){
if(clicked != 0){
//if other than 0, not followed
return false;
}
else{
//link is followed here since it's the first time it's being clicked and clicked value = 0
clicked = clicked + 1;
}
}
回答by tvanfosson
Using jQuery you can do this in a browser-independent either by replacing the href or by adding a click handler that preempts and stops the link being followed.
使用 jQuery,您可以在独立于浏览器的情况下执行此操作,方法是替换 href 或添加一个点击处理程序来抢占并停止跟随的链接。
$('#objLinkButton').attr('href','#').addClass('disabled-link');
or
或者
$('#objLinkButton').click( function() { return false; } )
.addClass('disabled-link');
Where the disabled-link
class has some CSS to change the look of the link so that it looks disabled visually.
凡disabled-link
类有一些CSS改变链接的外观,使其看起来禁用视觉。
Note that if this if the control is inside a naming container (like a GridView or UserControl), you'll have to reference the name using the "ending with" selector on the id.
请注意,如果控件位于命名容器(如 GridView 或 UserControl)内,则必须使用 id 上的“结尾”选择器引用名称。
$('id$="objLinkButton"')...
EDIT: Based on your update. Try this:
编辑:基于您的更新。尝试这个:
var code = null;
$(document).ready( function() {
var button = $('#objLinkButton');
code = button.attr('href').replace(/javascript:/,''); // save postback function
button.attr('href','#'); // replace postback function
button.click( function() {
$(this).unbind('click'); // get rid of click handler so it only fires once
if (code) { // if link hasn't been used
eval(code); // do post back
}
});
});
回答by codemonkeh
The simplest way to disable a link (and render it like normal text) without using jQuery is to remove it's href
attribute entirely.
在不使用 jQuery 的情况下禁用链接(并像普通文本一样呈现它)的最简单方法是href
完全删除它的属性。
For example here is the rendered link:
例如这里是呈现的链接:
<a id='link1' href="javascript:disableLink('link1');">Click me</a>
And the required JavaScript:
以及所需的 JavaScript:
function disableLink(id) {
document.all[id].removeAttribute('href');
}
This works for me in both IE and Firefox, but you may wish to do some more extensive testing.
这在 IE 和 Firefox 中都适用于我,但您可能希望进行一些更广泛的测试。
回答by James Reategui
I am building on tvanfosson's answer. His answer using jQuery works well, but if you have validators it messes things up. Here goes:
我建立在tvanfosson的回答之上。他使用 jQuery 的回答效果很好,但如果你有验证器,它就会把事情搞砸。开始:
var code = null;
$(document).ready(function () {
var button = $('#SubmitPaymentLnkBtn');
code = button.attr('href').replace(/javascript:/, '');
button.attr('href', '#'); // replace postback function
button.click(function () {
if (Page_ClientValidate('PaymentInfo')) {
// now only runs if above validationGroup is valid
$(this).unbind('click');
if (code) {
$(this).addClass('disabled-btn');
eval(code); // do post back
}
}
});
});
Then in your markup, be sure to set ClientIDMode to Static.
然后在您的标记中,确保将 ClientIDMode 设置为静态。
<asp:LinkButton ID="SubmitPaymentLnkBtn" Text=" submit " runat="server" CssClass="BlackBgText" ValidationGroup="PaymentInfo" Font-Size="22px" onclick="SubmitPaymentLnkBtn_Click" ClientIDMode="Static" />
回答by Hemal
If you want to disable a linkbutton, just use following code.
如果要禁用链接按钮,只需使用以下代码。
Markup
标记
<asp:LinkButton ID="lnkButton" Text="Submit" runat="server">
</asp:LinkButton>
C# Code
C# 代码
this.lnkButton.Attributes.Add("disabled","disabled");
回答by Joy Fernandes
Try this. Simplest Example
尝试这个。最简单的例子
<asp:LinkButton ID="LinkButton6" runat="server" disabled="disabled" OnClientClick="return false;"> Test Button</asp:LinkButton>