比如我在test.aspx里面写了一个JavaScript的函数test():
<script type="text/javascript">
function test()
{
alert("Test");
}
</script>
现在我想在test.aspx.cs某个文本框控件的textchange中调用这个函数
比如在:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//想在这里调用test()方法
}
应该怎么做呢?
这里先谢谢各位了!

解决方案 »

  1.   

    scriptManager.RegisterStartupScript(Control control,Type type,string key,string script,bool)
    进行脚本注册
      

  2.   

    回yumenlong5149 :
    似乎不行,我用以下方法不管用,执行该语句后,id为name的div内容没有改变:
    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "document.getElementById ('name').innerHTML='恭喜,该用户名可以注册';", true);
    但如果把
    document.getElementById ('name').innerHTML='恭喜,该用户名可以注册';
    修改为:
    alert('Test');
    则程序可以顺利执行
    不知道怎么回事,都是JavaScript代码
      

  3.   

    你试试这个String ScriptStr = "window.attachEvent('onload',function(){document.getElementById ('name').innerText='恭喜,该用户名可以注册'; });";
    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", scriptStr, true);
      

  4.   

    其中的string script是一段脚本,就是<script>alert('test')</script>,而不是客户端语句,对5楼说的基本正确,脚本注册就是在服务器端写代码,浏览器运行的时候会在客户端生成javascript代码,但是在服务器端动态向<div>中添加内容是可行的,借鉴技术文章http://topic.csdn.net/u/20090612/16/25df28a3-a491-43ea-aa8f-ad52ab4474eb.html?60809
      

  5.   

    后台调用前台不知道我知道前台JS可以调用后台的方法
    参考http://www.clubdotnet.com/dispbbs.asp?boardid=6&id=45&page=1&star=1
      

  6.   

    不知道你是要做什么 如果要在这个文本框的文字改变的时候调用test() 你可以在Page_Load()里添加一个TextBox1.Attributes.Add("onkeypress", "test()"); 这样就能实现了
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  7.   

    ClientScript.RegisterStartupScript(this.GetType(),"textchange","<script>test()</script>")
      

  8.   

    TextBox1.Attributes.Add("onkeypress", "test()");  
      

  9.   

    那为什么不在HTML中的TEXTBOX1中通过 onkeypress="test();" 或者 onchange="test();"呢?
      

  10.   


    如果你要用RegisterStartupScript这种方式来改变id为name的DIV内容,必须将此DIV放在 有"runat=server"的form中.其实你这样其实是舍近求远,直接用一个服务器端DIV控件,在后台直接用
    name.innerHTML="该用户可以注册",何必用后台输出javascript这种弯路
      

  11.   

    javascript代码:<script type="text/javascript" language="javascript">
         function test()
          {
             alert("oec2003");
             return false;
         } 
    </script>c#代码: protected void Button1_Click(object sender, EventArgs e){
         ClientScript.RegisterStartupScript(this.GetType(), "clear", "<script>test()</script>");
    }