How to fire JS from code behind of a UserControl by an event

In ASP.NET C#, I want to show an ALERT(‘HI’) from codebehind of a UserControl but doesn’t work:

User Control:

<asp:UpdatePanel ID="updatepanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click();"/>
    </ContentTemplate>
</asp:UpdatePanel>

ASP.NET Page

protected void btnSubmit_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "s1", "javascript:alert('hi!')", true);
}

UPDATE:

Forgot to say that the ASP.NET page itself is pop-out window by “prettyPhoto”.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

there may be below possible reasons try to change these and check it works or not

 1). ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('hi!')", true);//no need to use javascript with alert when bool set to true

 2). ScriptManager.RegisterStartupScript(page, page.GetType(), "msg", "alert('hi!')", true);

 3). ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('hi!');</script>", true);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply