2011-12-15 83 views
1

我做了一個web user control(.ascx),它由兩個html和兩個input buttons組成,當我嘗試做document.getElementById('<%=ControlID.ClientID%>')時它返回null。我可能做錯了什麼?getElementByID()for web user control not working?

源代碼:

<script language="javascript" type="text/javascript"> 

    function intilize() { 
     var x = document.getElementById('<%=JSNumeric_Control.ClientID%>'); 
    } 

</script> 

<table class="style1"> 
    <tr> 
     <td > 
     <My:UserInfoBoxControl ID="JSNumeric_Control" runat="server" /> 
      </td> 
     <td> 
      &nbsp;</td> 
    </tr> 
    <tr> 
     <td colspan="2" style="text-align: center"> 
      <input id="Button1" type="button" value="button" onclick="intilize()" /><br /> 
      </td> 
    </tr> 
</table> 

下面是Numbric.cs的代碼,以下是使用的屬性和頁面加載我使用JavaScript來分配所有的事件的HTML輸入:

public partial class NumricCounter : System.Web.UI.UserControl 
    { 
     public string defualtValue = "0"; 
     public double defaultIncrementValue = 1; 
     public int defaultPrecsionValue = 0; 

     public string Button_Add_ClientID 
     { 
      get { return Button_Add.ClientID; } 
     } 

     public string Button_Subtract_ClientID 
     { 
      get { return Button_Subtract.ClientID; } 
     } 

     //Set and Get for all these properties. (Code Omitted) 
     public string Text; 

     public double IncrementValue; 

     public int PrecsionValue; 

     public void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       Text_Output.Value = this.Text; 
       Button_Add.Attributes.Add("onclick", "Add(" + IncrementValue.ToString() + "," + PrecsionValue.ToString() + ");"); 
       Button_Subtract.Attributes.Add("onclick", "Subtract(" + this.IncrementValue.ToString() + "," + this.PrecsionValue.ToString() + ")"); 
       Text_Output.Attributes.Add("onkeyup", "check(" + this.PrecsionValue.ToString() + ");"); 
      } 
     } 
    } 
+0

你在哪裏放 「的document.getElementById( '<%= ControlID.ClientID%>')」 你能張貼的網頁表單代碼? – 2011-12-15 13:01:04

+0

你檢查了渲染後的頁面源代碼,看看你期望渲染的控件是否是?同時提供源代碼會使我們更容易回答問題。 – 2011-12-15 13:02:38

+1

你期待哪個元素得到回報?我不希望返回文本框或輸入按鈕...什麼是您的控件的HTML輸出(即後呈現)和它試圖查找的ControlID.ClientID的值是什麼?我在想,clientID可能只是作爲其他元素的前綴出現。 – Chris 2011-12-15 13:02:59

回答

3

嘗試更換你的函數:

function intilize() { 
     var x = document.getElementById('<%=JSNumeric_Control.ClientID%>'); 
    } 

與此:

function intilize() { 
     var x = document.getElementById('JSNumeric_Control'); 
    } 

getElementById應該從rendored HTML閱讀元素的ID。

0

你很可能試圖在DOM被加載之前搜索DOM。嘗試把你的代碼的onload處理器內部:

document.onload = function() { 
    alert(document.getElementById('<%=ControlID.ClientID%>')); 
} 

這可以確保之前您實際上都加載DOM的不執行代碼。

1

JSNumeric_Control.ClientID將返回控件的ClientID,如果它被呈現給頁面。它的存在並不一定意味着在具有該ID的最終頁面上會有HTML。

例如,如果您創建一個只輸出兩個按鈕的控件,您將爲每個按鈕提供與它們所在的控件不同的ID。通常,您可能會創建一個容器div,所有其他的內容,你會給控制的ID以便於查找,但沒有理由存在。

您應該做的是確保您的控件確實使用您的控件的ID創建了此HTML容器,或者您應該特別參考控件中的項目的客戶端ID。

var button1 = document.getElementById('<%=JSNumeric_Control.Button1.ClientID%>'); 
1

問題是,在DOM中沒有元素與您的UserInfoBoxControl的Id = ClientID。你的用戶控件中會有其他ID,如「JSNumeric_Control_Button1」,「JSNumeric_Control_TextBox1」等,如果你需要得到雙方的輸入按鈕,您可以執行下列操作之一:

  1. 使用jQuery選擇找到與類型=按鈕和id的所有輸入開始<%= JSNumeric_Control.ClientID%>

  2. 添加兩個新的屬性到您的控制 - FirstButtonClientID和SecondButtonClientID,將提供您與您的按鈕的ClientID。然後你可以在javascript中使用它。

  3. 創建自定義JavaScript對象,它將代表您的用戶控件並提供必要的功能。

0
//Button_Add is the control id inside the user control 
var x = document.getElementByID(JSNumeric_Control.FindControl("Button_Add").ClientID);