如何在客户端启用 asp:LinkBut​​ton

发布于 2024-11-10 01:50:46 字数 1935 浏览 4 评论 0原文

我有 asp:LinkBut​​ton,输入按钮定义为:

<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'));"  />

LinkBut​​ton 最初在代码隐藏中被禁用为:

    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 时; LinkBut​​ton 被呈现为

<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(&#39;lnkViewPdf&#39;,&#39;&#39;)">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 技术交流群。

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

发布评论

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

评论(6

夜未央樱花落 2024-11-17 01:50:46

现在尝试...

function TestEnable(lnkbutton) {
    lnkbutton.disabled = "";
    lnkbutton.onclick = "";
}

Try now...

function TestEnable(lnkbutton) {
    lnkbutton.disabled = "";
    lnkbutton.onclick = "";
}
清秋悲枫 2024-11-17 01:50:46

在后面的代码中,不要通过设置 Enabled = false 来禁用,而是设置:

    lnkViewPdf.Attributes["disabled"] = "disabled"

所以你的 javascript 函数:

    function TestEnable(lnkbutton) {
    alert('TestEnable() called');
    alert(lnkbutton.id);
    lnkbutton.disabled = "";
}

你的标记:

    <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.ClientID %>')); return false;"  />

和你的代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            lnkViewPdf.Attributes["disabled"] = "disabled";
    }

In the code behind, rather than disable by setting Enabled = false, set:

    lnkViewPdf.Attributes["disabled"] = "disabled"

So your javascript function:

    function TestEnable(lnkbutton) {
    alert('TestEnable() called');
    alert(lnkbutton.id);
    lnkbutton.disabled = "";
}

Your markup:

    <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.ClientID %>')); return false;"  />

And your code-behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            lnkViewPdf.Attributes["disabled"] = "disabled";
    }
爱的那么颓废 2024-11-17 01:50:46
$("#<%=lnkViewPdf.ClientID %>").removeAttr("disabled");
$("#<%=lnkViewPdf.ClientID %>").removeAttr("disabled");
山色无中 2024-11-17 01:50:46

您需要知道 .Net 构造的名称才能完成它。如果可以的话,最简单的方法是将其设置在页面的头部:

<script language="javascript">

var lnkbuttonToEnableId = "<%= this.lnkViewPdf.ClientId %>";

function TestEnable() {
        alert('TestEnable() called');
        lnkbuttonToEnableId.disabled = false;
}

</script>

无论如何,让它工作的唯一方法是以某种方式将 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:

<script language="javascript">

var lnkbuttonToEnableId = "<%= this.lnkViewPdf.ClientId %>";

function TestEnable() {
        alert('TestEnable() called');
        lnkbuttonToEnableId.disabled = false;
}

</script>

At any rate, the only way to get it to work is to pass the ClientId of lnkViewPdf to the function somehow.

丑丑阿 2024-11-17 01:50:46

尝试这两个:

<input id="Button2" type="button" value="TestEnable"
    onclick="TestEnable(document.getElementById('<%= lnkViewPdf.ClientID %>'));"  />

$("#<%= lnkViewPdf.ClientID %>").removeAttr('disabled'); 

更新:由于您在服务器端禁用LinkBut​​ton,.NET会从<中删除href属性;a> html 元素。为了防止丢失该信息,您应该做的是禁用客户端上的 LinkBut​​ton,然后在需要时启用它。此外,您不需要禁用它,只需删除 href 属性即可。

因此,首先您需要保留 href 并将其删除,以便 链接被禁用:

$(document).ready(function () {
    var $lnkViewPdf = $("#lnkViewPdf");

    $lnkViewPdf.data("href", $lnkViewPdf.attr("href"));
    $lnkViewPdf.removeAttr("href");
});

以及启用它的函数:

function TestEnable(lnkViewPdfId) {
    var $lnkViewPdf = $("#" + lnkViewPdfId);

    $lnkViewPdf.attr("href", $lnkViewPdf.data("href"));
}

try both of those:

<input id="Button2" type="button" value="TestEnable"
    onclick="TestEnable(document.getElementById('<%= lnkViewPdf.ClientID %>'));"  />

or

$("#<%= lnkViewPdf.ClientID %>").removeAttr('disabled'); 

UPDATE: Since you are disabling the LinkButton on server side .NET strips the href attribute from the <a> html element. What you should do to prevent the lost of that information is to disable the LinkButton on the client and then enable it when you need to. Also instead of disabling it all you need to do is remove the href attribute.

So first you need to retain the href and remove it so the <a> link become disabled:

$(document).ready(function () {
    var $lnkViewPdf = $("#lnkViewPdf");

    $lnkViewPdf.data("href", $lnkViewPdf.attr("href"));
    $lnkViewPdf.removeAttr("href");
});

and the function that enables it:

function TestEnable(lnkViewPdfId) {
    var $lnkViewPdf = $("#" + lnkViewPdfId);

    $lnkViewPdf.attr("href", $lnkViewPdf.data("href"));
}
悲欢浪云 2024-11-17 01:50:46

如果您使用以下方法禁用 LinkBut​​ton:

if (!IsPostBack)
{
    this.lnkViewPdf.Enabled = false;
}

那么 href 属性将不会显示在 HTML 中。如果您手动添加禁用属性:

if (!IsPostBack)
{
    lnkViewPdf.Attributes.Add("disabled", "disabled");
}

那么您的代码将正常工作。

哦!...最后一件事:您需要设置 LinkBut​​ton 的 PostBackUrl 属性。你在你的例子中错过了它。

If you disable the LinkButton using:

if (!IsPostBack)
{
    this.lnkViewPdf.Enabled = false;
}

Then the href attibute won't be displayed in the HTML. If you manually add the disabled attribute instead:

if (!IsPostBack)
{
    lnkViewPdf.Attributes.Add("disabled", "disabled");
}

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.

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