Thursday, 18 December 2014

Access a parent element (Button) from iframe using c#

Access parent element (Button) from iframe using c#


Suppose you have 2 pages:
1. Parent Page: On which there is a iframe. as Parent.aspx
2. Loaded page: inside the iframe where Child.aspx is loaded

You want if you save some thing on Child.aspx (Loaded page), the grid on Parent page (Parent.aspx) will automatically refresh.  

So you make a hidden button on the Parent page (Parent.aspx)

<div style="display:none">
    <asp:Button ID="Done" runat="server" ClientIDMode="Static" OnClick="Done_Click" />
</div>

protected void Done_Click(object sender, EventArgs e)
    {
        // Refresh Grid
    }

Note that the ClientIDMode="Static" because from the other page you can not know what is the rendered id, so keep it static. If you do not use dot net 4 that have the static id, then you need to also use javascript to send the rendered id to the page, or some other way to 
locate the button.

Call a javascript function on Child.aspx (Loaded Page)

<script type="text/javascript">
function ParentRefresh()
{
   window.parent.document.getElementById('Done').click();
}
</script>

protected void Save_Click(object sender, EventArgs e)
    {
// your save code
ScriptManager.RegisterStartupScript(this, GetType(), "message", "ParentRefresh();", true);
}

No comments:

Post a Comment