2011-05-26 59 views
0

我有ASP:LinkBut​​ton的,定義的輸入按鈕:如何啓用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 
    } 

但我不能啓用鏈接按鈕。

我錯過了什麼嗎?

謝謝!

__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __

任何有興趣在解決上述問題:
在後臺代碼:

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如上圖所示,呈現的LinkBut​​ton爲:

<a id="lnkViewPdf" class="icoMiniTest" disabled="disabled" onclick ="return false" href="javascript:__doPostBack(&#39;lnkViewPdf&#39;,&#39;&#39;)">View Office Pdf</a> 

HTH。

+0

禁用LinkBut​​ton控件服務器端 – 2011-05-26 16:15:34

回答

1

現在就來試試...

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

甚至設置lnkbutton.disabled =「假」後,不工作的時候改變了我的解決方案,以適應.NET的行爲; //沒有回覆 – iniki 2011-05-26 15:30:24

+0

一個語句失蹤,以啓用onclick ..現在嘗試.. – 2011-05-26 15:33:26

+0

@iniki;你現在試試這個嗎? – 2011-05-26 15:59:05

0

你需要爲了完成它知道的.Net構建的名稱。最簡單的方法是有,如果你能在它的頁面頭部設置:

<script language="javascript"> 

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

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

</script> 

在任何情況下,要得到它的工作的唯一途徑是lnkViewPdf的客戶端Id傳遞給函數莫名其妙。

+0

不工作:-(甚至嘗試使用var lnkbuttonToEnableId =「<%= this.lnkViewPdf.ClientId%>」;但獲取錯誤: LinkBut​​ton不包含'ClientId的定義 – iniki 2011-05-26 15:47:57

+0

@iniki:由於您指定了'ClientIDMode = 「Static」你應該可以跳過這一步,直接進入'$(「#lnkViewPdf」)。removeAttr('disabled');'。該屬性指示頁面呈現過程爲控件提供與您在ID中指定的相同的ClientID。 – 2011-05-26 16:16:32

0

都試一下那些:

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

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

UPDATE:因爲你是在服務器端.NET禁用LinkButton條從<a> HTML元素href屬性。你應該做什麼來防止這些信息的丟失,是禁用客戶端上的LinkButton,然後在需要時啓用它。而不是禁用它,所有你需要做的就是刪除href屬性。

所以首先你需要保留href並刪除它,因此<a>鏈接被禁用:

$(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")); 
} 
+0

無法正常工作:-( – iniki 2011-05-26 15:46:46

1

在後面的代碼,而不是通過設置禁用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"; 
    } 
+0

嘗試過,但設置lnkViewPdf.Attributes [「disabled」] =「disabled」;在後面的代碼中並沒有實際禁用LinkBut​​ton ...將它的結果點擊回發。 – iniki 2011-05-26 16:03:22

0

如果使用禁用的LinkBut​​ton:

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

然後href attibute不會顯示在HTML中。如果您手動添加,而不是禁用屬性:

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

那麼你的代碼將工作得很好。

哦!..還有最後一件事:您需要爲LinkBut​​ton設置PostBackUrl屬性。你在例子中錯過了它。

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