如题,还有怎么在服务器控件上用js函数

解决方案 »

  1.   

    服务器控件的onclientclick事件直接指向js函数名就可以了
      

  2.   

        <asp:DropDownList ID="DropDownList1" onchange="txtbox1.value=this.value" runat="server">
        </asp:DropDownList>
      

  3.   

    这样写:protected void Page_Load(object sender, EventArgs e)
    {
        this.DropDownList1.Attributes["onclick"] = "document.getElementById('" +
            this.TextBox1.ClientID + "').value=this.value;";
    }
    asp.net程序本来就是用来动态生成html/javascript的,如果你再只会在html上“写死”javascript那么岂不是白学了asp.net?
      

  4.   

    支持3楼。。
    另外用js改变服务器控件textbox的值貌似会出问题,如果textbox本身有text属性的话(js不能改变viewstate里保存的text值)
      

  5.   

    DropDownList1.Attributes.Add( "onchange","A(this.options[this.selectedIndex].text)");  
    funcation A(s)
    {document.getElementById('<%=TextBox1.ClientID %>').value=s;
     }
      

  6.   

    使用 this.TextBox1.ClientID,是因为我们不能过于相当于地假设客户端id一定是textbox1。稍微复杂你一点,例如放入ascx、放入模板列、放入使用masterpage的页面等等,就会发现ClientID的概念才是持久的。客户端<input />的value会回填服务器端Text,然后正好因为与ViewState中的旧的值不同而触发TextChanged事件。