如何在客户端启用 asp:LinkButton
我有 asp:LinkButton,输入按钮定义为:
<asp:LinkButton ID="lnkViewPdf" runat="server" CssClass="icoMiniTest" ClientIDMode="Static" >View Office Pdf</asp:LinkButton>
<input id="Button2" type="button" value="TestEnable" onclick="TestEnable(document.getElementById('lnkViewPdf'));" />
LinkButton 最初在代码隐藏中被禁用为:
if (!IsPostBack)
{
this.lnkViewPdf.Enabled = false;
}
并且需要在单击 Button2 时启用,所以我调用 javascript 函数来启用链接为:
function TestEnable(lnkbutton) {
alert('TestEnable() called');
alert(lnkbutton.id);
lnkbutton.disabled = "";
//$("#lnkbutton").removeAttr('disabled'); //even this doesn't work
}
但我无法启用链接按钮。
我错过了什么吗?
谢谢你!
____________< /em>_________________< em>_____________________
任何对上述问题的解决方案感兴趣的人:
在代码隐藏中:
this.lnkViewPdf.Attributes["disabled"] = "disabled";
this.lnkViewPdf.Attributes["onclick "] = "return false";
.js:
function TestEnable(lnkbutton) {
$(lnkbutton).removeAttr('disabled');
lnkbutton.onclick = "";
}
注意:设置 lnkViewPdf.Enabled = false 时; LinkButton 被呈现为
<a id="lnkViewPdf" class="aspNetDisabled icoMiniTest">View Office Pdf</a>
样式类 aspNetDisabled,由 ASP.Net 添加的内容
但是,如上所示,从代码隐藏中设置禁用/onclick 属性,将 Linkbutton 渲染为:
<a id="lnkViewPdf" class="icoMiniTest" disabled="disabled" onclick ="return false" href="javascript:__doPostBack('lnkViewPdf','')">View Office Pdf</a>
HTH。
I have asp:LinkButton, input Button defined as:
<asp:LinkButton ID="lnkViewPdf" runat="server" CssClass="icoMiniTest" ClientIDMode="Static" >View Office Pdf</asp:LinkButton>
<input id="Button2" type="button" value="TestEnable" onclick="TestEnable(document.getElementById('lnkViewPdf'));" />
the LinkButton is initially disabled in code-behind as:
if (!IsPostBack)
{
this.lnkViewPdf.Enabled = false;
}
and needs to be enabled when Button2 is clicked, so I am calling javascript function to enable the link as:
function TestEnable(lnkbutton) {
alert('TestEnable() called');
alert(lnkbutton.id);
lnkbutton.disabled = "";
//$("#lnkbutton").removeAttr('disabled'); //even this doesn't work
}
But I am not able to enable the linkbutton.
Am I missing something?
Thank you!
__________________________________________________
Anyone interested in solution to above problem:
In code-behind:
this.lnkViewPdf.Attributes["disabled"] = "disabled";
this.lnkViewPdf.Attributes["onclick "] = "return false";
.js:
function TestEnable(lnkbutton) {
$(lnkbutton).removeAttr('disabled');
lnkbutton.onclick = "";
}
NOTE: When setting lnkViewPdf.Enabled = false; LinkButton was being rendered as
<a id="lnkViewPdf" class="aspNetDisabled icoMiniTest">View Office Pdf</a>
see the style class aspNetDisabled, something added by ASP.Net
However setting disabled/onclick attributes from the codebehind as shown above, render Linkbutton as:
<a id="lnkViewPdf" class="icoMiniTest" disabled="disabled" onclick ="return false" href="javascript:__doPostBack('lnkViewPdf','')">View Office Pdf</a>
HTH.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
现在尝试...
Try now...
在后面的代码中,不要通过设置 Enabled = false 来禁用,而是设置:
所以你的 javascript 函数:
你的标记:
和你的代码隐藏:
In the code behind, rather than disable by setting Enabled = false, set:
So your javascript function:
Your markup:
And your code-behind:
您需要知道 .Net 构造的名称才能完成它。如果可以的话,最简单的方法是将其设置在页面的头部:
无论如何,让它工作的唯一方法是以某种方式将 lnkViewPdf 的 ClientId 传递给函数。
You need to know the .Net constructed name in order to accomplish it. The easiest way is to have it set in the head of the page if you can:
At any rate, the only way to get it to work is to pass the ClientId of lnkViewPdf to the function somehow.
尝试这两个:
或
更新:由于您在服务器端禁用
LinkButton
,.NET会从<中删除
html 元素。为了防止丢失该信息,您应该做的是禁用客户端上的href
属性;a>LinkButton
,然后在需要时启用它。此外,您不需要禁用它,只需删除href
属性即可。因此,首先您需要保留
href
并将其删除,以便链接被禁用:
以及启用它的函数:
try both of those:
or
UPDATE: Since you are disabling the
LinkButton
on server side .NET strips thehref
attribute from the<a>
html element. What you should do to prevent the lost of that information is to disable theLinkButton
on the client and then enable it when you need to. Also instead of disabling it all you need to do is remove thehref
attribute.So first you need to retain the
href
and remove it so the<a>
link become disabled:and the function that enables it:
如果您使用以下方法禁用 LinkButton:
那么 href 属性将不会显示在 HTML 中。如果您手动添加禁用属性:
那么您的代码将正常工作。
哦!...最后一件事:您需要设置 LinkButton 的 PostBackUrl 属性。你在你的例子中错过了它。
If you disable the LinkButton using:
Then the href attibute won't be displayed in the HTML. If you manually add the disabled attribute instead:
Then you're code will work just fine.
Oh!.. and one last thing: You need to set the PostBackUrl property for the LinkButton. You missed it in your example.