日期:2014-05-19  浏览次数:20695 次

在Web页面中,在<script language="javascript">里面如何访问.net的控件啊?
比如:
有一Html的按键:
<INPUT   style= "Z-INDEX:   103;   LEFT:   520px;   POSITION:   absolute;   TOP:   48px "   type= "button "   value= "Button " onclick= "a(); ">

有一asp的text:
<asp:TextBox   id= "TextBox1 "   style= "Z-INDEX:   102;   LEFT:   504px;   POSITION:   absolute;   TOP:   88px "   runat= "server "> </asp:TextBox>

想在 <script   language= "javascript "> 里面写一方法:(功能:按一下Button,在TextBox1里面显示一个字符串)
<script   language= "javascript ">
function   a(){
alert( 'asdj! ');
all.TextBox1.text= "aaaa! ";(注:报错:找不到“TextBox1”)
}
</script>
这样的方法如何写?

------解决方案--------------------
document.getElementById( "TextBox1 ")
------解决方案--------------------
document.getElementById( "TextBox1 ").value= "aaaa! ";
------解决方案--------------------
web控件就是Html控件两者没有区别
------解决方案--------------------
<script language= "javascript ">
window.onload=init;
function init()
{
var TextBox1=document.getElementById( "TextBox1 ");
TextBox1.onclick=a;
}
function a(){
alert( 'asdj! ');
this.text= "aaaa!;
}
</script>

很完美
在 <input 标记里也不要写 onclick= "a(); "这个了
------解决方案--------------------
document.getElementById( "TextBox1 ").value = "aaaa ";
or
document.form1.TextBox1.value = "aaaa ";


------解决方案--------------------
注意一下,web控件写到页面的时候名字不一定是原来的ID,如果他被包括在某个对象里他的名字应该是 "上层名_自己名"
------解决方案--------------------
也就就是clientID,而不是ID
------解决方案--------------------
VS2005

document.getElementById( ' <%=TextBox1.ClientID%> ').value= "xx ";
------解决方案--------------------
alert(document.getElementById( "TextBox1 "));
document.getElementById( "TextBox1 ").innerText = "aaaa! ";或
document.getElementById( "TextBox1 ").value = "aaaa! ";

正解!!