Reputation: 5424
I want to show a javascript alert when a user changes a value in a select-box. This is my code, what am i doing wrong?
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')");
}
Upvotes: 1
Views: 5620
Reputation: 25352
try this
Add
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
to your designer part
and in code part add
var ID="Whatever the data is";
ClientScript.RegisterStartupScript(this.GetType(), "script", "alert('"+ID+"');", true);
If you are using update panel
try this
var ID="Whatever the data is";
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('"+ID+"');", true);
Upvotes: 2
Reputation: 8584
Have you tried
<asp:DropDownList ID="ddlGroups" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlWagons_SelectedIndexChanged" clientIdMode="static"></asp:DropDownList>
and in JS:
document.getElementById("ddlGroups").addEventListener("change", function(){
alert("new value: " + this.options[this.selectedIndex].value);
});
Upvotes: 2
Reputation: 82136
You should be able to handle the onchange
event before the postback occurs
<asp:DropDownList runat="server" onchange="alert('Clicked')" />
If you don't need a postback then don't use a server control.
Upvotes: 2