Javascript function is not getting call from code behind when it is put on master page
Asked Answered
D

1

0

I have 2 pages named as :Abc.aspx and popupAbc.aspx

Now on Abc.aspx i have below button on click of which i am opening popupAbc.aspx as pop up:

<asp:Button ID="btnOpenChildAbcPopup" runat="server" Text="Open" UseSubmitBehavior="false" CausesValidation="False" OnClick="btnOpenChildAbcPopup_Click" />

Code Behind:

protected void btnOpenChildAbcPopup_Click(object sender, EventArgs e)
        {
            string url="../popupAbc.aspx";
            ScriptManager.RegisterStartupScript(Page, GetType(), "alert", "OpenChildAbcPopup('" + url + "');",
                    true);
        }

function OpenChildAbcPopup(url) {
                    $.fancybox({
                        'onStart ': function () { $.fancybox.hideActivit() },
                        'onComplete': function () { $.fancybox.hideActivity() },
                        'titleShow': 'true',
                        'titlePosition': 'over',
                        'titleFormat': 'formatTitle',
                        'href': url,//popupAbc.aspx
                        'type': 'iframe',
                        'width': '1000',
                        'height': '500',
                        'fitToView': false,
                        'autoSize': false,
                        'hideOnOverlayClick': false,
                        'hideOnContentClick': false,
                        'overlayOpacity': 0.7,
                        'enableEscapeButton': false,
                        'closeEffect': 'none' 
                    });
                    $.fancybox.hideLoading();
                    return false;
                }

Here up till now everythings works perfectly and on click of button my popupAbc.aspx opens perfectly in pop up box but problem is with in this popupAbc.aspx.

i have one button in popupAbc.aspx in which i will save some data and after saving data i would like to again call the javascript function which is on master page but that javascript function is not calling.

This is my page:popupAbc.aspx

  <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MyMaster.Master" CodeBehind="popupAbc.aspx.cs" Inherits="popupAbc.aspx" %>

<asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" UseSubmitBehavior="true"  ValidationGroup="p1" />

 protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //code for saving form data in database and after that call javascript function which
            //is on MyMaster page
            ScriptManager.RegisterStartupScript(Page, GetType(), "Js", "ExitMyCurrentFancyBox();", true);
        }

But in firebug console it is throwing error that ExitMyCurrentFancyBox is not defined.

When i put this ExitMyCurrentFancyBox function in my popupAbc.aspx then it is calling successfully but when i put this on my master page then it is not calling.

function ExitMyCurrentFancyBox() {
                alert()
            }

Note:My Abc.aspx uses update panel and popupAbc.aspx doesnt uses update panel.

Can anybody tell me why that Javascript function is not calling when i put it on master page and how to call it when it is on master page??

I have tried all this but still any of below is not working:

 // ScriptManager.RegisterStartupScript(Page, GetType(), "alert", "ExitMyCurrentFancyBox();", true);
               // ScriptManager.RegisterStartupScript(Page, GetType(), "alert", "ExitMyCurrentFancyBox();", true);
                //ClientScript.RegisterClientScriptBlock(GetType(), "sas", "CloseFancyboxtl();", true);
              //  ScriptManager.RegisterStartupScript(this, this.GetType(), "ntmtch", "ExitMyCurrentFancyBox();", true);
          //  ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg", "ExitMyCurrentFancyBox();", true);
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "ExitMyCurrentFancyBox;", true);
            ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), Guid.NewGuid().ToString(), "ExitMyCurrentFancyBox();", true);
Disinterested answered 30/1, 2016 at 6:46 Comment(2)
because it is not public method. You may create some public method, which executes your javascript code, and call that method from your child page insteadTransvestite
Thanks for the reply but can you please show how to do it.can you please post some solutionDisinterested
T
1

Posting possible solution due to your request.

Add next method to your MasterPage:

public void ShowMyJavascript(){
    Response.Write("<script language='javascript'>alert('Here is my message to be displayed as an alert');</script>");
}

And in your content page:

this.Master.ShowMyJavascript();
Transvestite answered 31/1, 2016 at 1:56 Comment(4)
i just want to call my ExitMyCurrentFancyBox js function instead of writing scriptDisinterested
@Learning I've found how to fix your code. Change your register to: ScriptManager.RegisterStartupScript(Page, GetType(), "js", "ExitMyCurrentFancyBox()", true); and it will work. Also check that your javascript is inside the head tag of master page, inside <script> tags. and also check the syntax to make sureTransvestite
after using this:ScriptManager.RegisterStartupScript(Page, GetType(), "js", "ExitMyCurrentFancyBox()", true);I am getting this error in my firebug console:SyntaxError: missing ; before statement.I am just firing alert in my js function and i have referenced all the js in head tagDisinterested
write here your javascript function pleaseTransvestite

© 2022 - 2024 — McMap. All rights reserved.