I have come across a issue calling javascript from within my UpdatePanel, that is within an iFrame. I can call the parent page javascript without issue from within the iFrame using parent.functionName().
This is inside my pageName.aspx.cs. (Code by KP ( ASP.Net - Javascript inside AJAX UpdatePanel ))
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
StringBuilder scriptStringB = new StringBuilder();
scriptStringB.Append("<script type=\"text/javascript\">");
scriptStringB.Append("function CloseiFrame() {");
scriptStringB.Append(" alert(\"CloseiFrame\");");
scriptStringB.Append(" CloseiFrameThroughParent();");
scriptStringB.Append("}");
scriptStringB.Append("</script>");
RegisterClientStartupScript("CloseiFrame", scriptStringB.ToString());
}
private void RegisterClientStartupScript(string scriptKey, string scriptText)
{
ScriptManager sManager = ScriptManager.GetCurrent(this.Page);
if (sManager != null && sManager.IsInAsyncPostBack)
{
//if a MS AJAX request, use the Scriptmanager class
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), scriptKey, scriptText, false);
}
else
{
//if a standard postback, use the standard ClientScript method
scriptText = string.Concat("Sys.Application.add_load(function(){", scriptText, "});");
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), scriptKey, scriptText, false);
}
}
I have tried other ways, using the Microsoft way ( http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx ) for example.
In pageName.aspx it looks like this:
<form id="Form1" class="iFrameForm" method="post" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
...Scripts are included here
</Scripts>
</asp:ScriptManager>
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<ContentTemplate>
<asp:imagebutton id="ExitButton" runat="server"
ImageUrl="~/exit.png"
AlternateText="<%$ Resources:Resource, Exit %>" ToolTip="<%$ Resources:Resource, Exit %>" OnClick="Exit_Click" >
</asp:imagebutton>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Of course there is more in there, but these are the basics. The button is generated and at the end of the form 'action' it has "jscriptfunction=CloseiFrame".
But there is no alert, let alone the function being called. I could of course use a service to work around all of this, but I would really like to solve this issue within the pages&javascript.
'A' solution has been found. Using the Page_Load to check if it is a postback or a normal pageload I can call on a javascript function from there.
protected override void Page_Load(object sender, System.EventArgs e)
{
base.Page_Load(sender, e);
if (IsPostBack) //If it is a postback close the popup
{
StringBuilder scriptStringB = new StringBuilder();
scriptStringB.Append("<script type=\"text/javascript\">");
scriptStringB.Append(" CloseiFrameThroughParent();");
scriptStringB.Append("</script>");
RegisterClientStartupScript("CloseiFrameThroughParent", scriptStringB.ToString());
}
}
But I must say, it isn't pretty...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.