2013-04-22 29 views
3

沒有得到的ClientID我有ASP.Net頁我在哪裏調用JavaScript函數如下:在ASP.Net

Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox> 
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value)" Text ="GO!" /> 

但在點擊GO按鈕,我收到以下錯誤:

的JavaScript運行時錯誤:無法獲取的未定義或空引用

屬性「價值」上觀看所呈現的代碼的按鈕:

<input type="submit" name="btn_view" value="GO!" onclick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value);" id="btn_view" /> 

它沒有放置適當的ClientID。我試着將CLientIDMode設置爲靜態的測試框,但沒有幫助。

此問題與unanswered question類似。

+0

我認爲這發生,因爲當你呈現你的ASP。網絡文本框控件,他是一個不同的id,而不是你在編輯html時看到的內容。 – 2013-04-22 12:42:24

+0

看看這是否可以幫助你,http://stackoverflow.com/questions/8844675/asp-net-document-getelementbyid-control- clientid-returns-null – 2013-04-22 12:43:30

回答

6

這個問題有幾個解決方案。

其中一個更簡單的方法是將名稱移動到JavaScript變量中,因爲當ASP.NET控件的OnClientClick屬性中嘗試評估txt_name.ClientID時,問題僅出現在控件中。

<script> 
var txtName = '<%= txt_name.ClientID %>'; 
</script> 

Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox> 
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" 
OnClientClick="return AlertOnGo('View Configuration', 
document.getElementById(txtName).value)" Text ="GO!" /> 
0

- 編輯答案 - ASPX

 
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="javascript:return AlertOnGo('View Configuration');" Text ="GO!" /> 

腳本

 
function AlertOnGo(mode) 
{ 
    var btnId = '<%= txt_name.ClientID %>'; 
    var value=document.getElementById(btnId).value; 

//your code 

return false; 
} 
+0

這沒有奏效。同樣的問題。零件'<%= txt_name.ClientID%>'不會像在網頁上一樣執行和渲染。 – 2013-04-22 12:51:59

+0

檢查編輯的答案 – 2013-04-22 13:31:19

+0

我認爲這將起作用。但我需要用不同的文本框ID調用相同的JavaScript函數,因此需要將該部分移出。見雅各的回答。它可以在每次調用AlertOnGo函數前設置txtName。 – 2013-04-22 14:27:30

0

在一個asp.net web控件的屬性使用<%=%>特殊照顧 - 我不會期望這工作,因爲整個控件將被html替換。

我會將您的JavaScript移出到一個腳本塊中,並在代碼中附加事件處理程序,而不是將它全部內聯。

Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox> 
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" Text ="GO!" /> 
<script> 
document.getElementById('<%= btn_view.ClientID %>').attachEvent('click', function() { 
    return AlertOnGo('View Configuration', 
         document.getElementById('<%= txt_name.ClientID %>').value); 
}); 
</script> 
3

您可以使用屬性ClientIDMode="Static"的ASP內部:文本標籤

這樣

<asp:TextBox ClientIDMode="Static" ID="txt_name" runat="server"></asp:TextBox> 
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" 
OnClientClick="return AlertOnGo('View Configuration', 
document.getElementById(txtName).value)" Text ="GO!" /> 

這樣的文本框的客戶端ID是一樣的「ID」和你可以用 document.getElementById('txt_name').value

它適用於我

0

我更喜歡使用String.Concat。您可以使用\ u0027而不是撇號。

OnClientClick='<%#String.Concat("document.getElementById(\u0027", AddToCartBtn.ClientID.ToString(), "\u0027).value = \u0027Adding...\u0027;")%>' 
0

另一種方案是,你可以去瀏覽器做昆蟲元素讀取文本框的動態標識,並設置在javascript,如:

var ddlFunction = document.getElementById('ctl00_SPWebPartManager1_g_ecede3f6_2b87_405a_af1b_70401bb8d4c7_ddlFunction'); 

HTML:

<asp:DropDownList ID="ddlFunction" runat="server" CssClass="dropDownClass"> 
    <asp:ListItem Selected="True">Select One</asp:ListItem> 
    <asp:ListItem>Sample 1</asp:ListItem> 
    <asp:ListItem>Sample 2</asp:ListItem> 
</asp:DropDownList>