2

I have master page. In master page I have one UserControl. In this UserControl I have one button . When I complete everything with this button event I want to call Javascript function from page. Below is my code :

Button Event

    protected void btnAddCompany_Click(object sender, EventArgs e)
    {
        // Do something Here

        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "alertmessage();", true);
    }

Javascript Function On same UserControl

    <script type="text/javascript">
   function alertmessage() {
       alert("Call Successfull");
   }
 </script>

But this javascript function never been called. When same UserControl is inside page it works. but if it is inside masterpage it doesn't.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
Hiren
  • 1,381
  • 5
  • 24
  • 41

1 Answers1

2

The above code works. Make sure if you have user ScriptManager.RegisterStartupScrip anywhere other part of the page then you have terminate Javascript code with ;. If inline javascript fails, no other function is called.

Refer: ScriptManager.RegisterStartupScript code not working - why?

Community
  • 1
  • 1
Pradip Prajapati
  • 152
  • 1
  • 4
  • 18
  • 6
    `RegisterStartupScript` did not work for me, but have changed it to `RegisterClientScriptBlock` and it worked, BUT only after changing `this` to `this.Page` (which is the subpage), The Master page itself doesn't store any scripts, it renders it to the sub page this you have to register it to the sub page. Example: `ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "myscr", "alert('test');", true);` – Pierre Feb 04 '15 at 11:24
  • 2
    Many Thanks @Pierre. Post it as answer. It would be help for other developers too. – Purohit Hitesh Dec 06 '17 at 14:03