如何从aspx.cs调用javascript方法

本文关键字:调用 javascript 方法 cs aspx | 更新日期: 2023-09-27 18:31:04

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

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

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

            }

如何从aspx.cs调用javascript方法

将列表框的"自动回发"属性设置为"true"并使用Page.ClientScript.RegisterStartupScript(GetType(), "whatiskey", "hello();", true);对我有用

使用

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>