Search code examples
javascriptasp.netserver-side

fire javascript function from asp.net codebehind


This is my javascript function ,

 function returnToParent() {            
        var oArg = new Object();
        var oWnd = GetRadWindow();
        oArg.ReturnValue = "Submit";
        oWnd.close(oArg);
    }

And this is how I call this function on client side

 <button title="Submit" runat="server" id="close" onclick="returnToParent(); return false;">
                    OK</button>

I want to fire this function in server side button click event .
What I've done is add new button

 <asp:Button runat="server" ID="rtxtSubmitChange" OnClick="rtxtSubmitChange_Click"  Text="Submit" />

and in ButtonClick Event ,

protected void rtxtSubmitChange_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(GetType(),
"MyKey",
"returnToParent();",
false);
    }

But It doesn't work . What I am wrong in my code ?


Solution

  • Try

    ScriptManager.RegisterStartupScript(this, this.GetType(), this.ClientID, "returnToParent()", true);
    

    OR

    ScriptManager.RegisterStartupScript(Page, Page.GetType(), this.ClientID, "returnToParent()", true);
    

    For more details refer :ScriptManager.RegisterStartupScript Method