我做了一個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>
</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() + ");");
}
}
}
你在哪裏放 「的document.getElementById( '<%= ControlID.ClientID%>')」 你能張貼的網頁表單代碼? – 2011-12-15 13:01:04
你檢查了渲染後的頁面源代碼,看看你期望渲染的控件是否是?同時提供源代碼會使我們更容易回答問題。 – 2011-12-15 13:02:38
你期待哪個元素得到回報?我不希望返回文本框或輸入按鈕...什麼是您的控件的HTML輸出(即後呈現)和它試圖查找的ControlID.ClientID的值是什麼?我在想,clientID可能只是作爲其他元素的前綴出現。 – Chris 2011-12-15 13:02:59