在网络表单方法中运行 asp.net javascript方法

Run javascript method in asp.net webforms method

本文关键字:方法 asp net javascript 运行 网络 表单      更新时间:2023-09-26

我想在用户更改选择框中的值时显示javascript警报。这是我的代码,我做错了什么?

.aspx

<asp:DropDownList ID="ddlGroups" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlWagons_SelectedIndexChanged"></asp:DropDownList>

aspx.cs

    protected void ddlWagons_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClientScriptManager script = Page.ClientScript;
        script.RegisterStartupScript(this.GetType(), "OpenWindowScript", "alert('Clicked')");
    }

试试这个

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

到您的设计师零件

并在代码部分添加

var ID="Whatever the data is";
ClientScript.RegisterStartupScript(this.GetType(), "script", "alert('"+ID+"');", true);

如果您使用的是更新面板

试试这个

var ID="Whatever the data is";
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('"+ID+"');", true);

您应该能够在回发发生之前处理onchange事件

<asp:DropDownList runat="server" onchange="alert('Clicked')" />

如果不需要回发,则不要使用服务器控件。

你试过吗

<asp:DropDownList ID="ddlGroups" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlWagons_SelectedIndexChanged" clientIdMode="static"></asp:DropDownList>

在 JS 中:

document.getElementById("ddlGroups").addEventListener("change", function(){
    alert("new value: " + this.options[this.selectedIndex].value);
});