2016-01-20 79 views
1

我想在aspx.cs(c#)中调用JavaScript中的方法hello(),当选择listbox1项目时。使用此代码执行操作但不起作用如何从aspx.cs中调用javascript方法

protected void ListBox1_TextChanged(object sender, EventArgs e) 
    { 
     ClientManager.RegisterStartupScript(this, GetType(), "whatiskey","hello();", true); 
    } 

function hello() { 
alert("hiiiii"); 
var arr = ["<%=myvalue %>"]; 


      } 
+0

不工作意味着什么?您是否收到任何错误消息 –

+0

没有错误,只是方法没有执行 – phpnet

+0

尝试:Page.ClientScript.RegisterStartupScript(GetType(),“whatiskey”,“hello();”,true); – Zaki

回答

1

使用

Response.Write("<script>hello();</script>"); 

编辑

,如果你想要做的就是呼吁项目的选择一个javascript,你可以使用onchange属性如下 - 列表框的

<asp:ListBox onchange="hello();" ID="ListBox1" runat="server"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 

    </asp:ListBox> 
    <script> 
     function hello() { 
      alert("hello"); 
     } 
    </script> 
+0

不工作,任何其他方式? – phpnet

+0

你有没有将ListBox的EnableAutoPostBack属性设置为true? –

+0

谢谢工作正常 – phpnet

2

设置“的AutoPostBack”属性设置为“真”,用Page.ClientScript.RegisterStartupScript(GetType(), "whatiskey", "hello();", true);为我工作

+0

对不起,标记为未答复,页面正在刷新,但方法未被调用 – phpnet

+0

@phpnet请问您可以提供“hello”方法声明吗?它在哪里宣布? – denisv

+0

请参阅更新 – phpnet