簡體   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