繁体   English   中英

在asp.net Webforms方法中运行javascript方法

[英]Run javascript method in asp.net webforms method

当用户在选择框中更改值时,我想显示一个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')");
    }

您应该能够在发生回发之前处理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);
});

尝试这个

<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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM