簡體   English   中英

如何從aspx.cs調用javascript方法

[英]How to call a javascript method from aspx.cs

我想在選擇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 %>"];


            }

將ListBox的“AutoPostBack”屬性設置為“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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM