Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have page1.aspx and page2.aspx and both page have buttons, in page1.aspx i use iframe and call page2.aspx, from page2 button i need to get page1 button id and trigger click event.
I used external javascript ,so both page can access if refer that script in both page.

What I have tried:

I used to get control id in following ways
document.getElementById("mySidenav")

document.getElementById("ContentPlaceHolder1_mySidenav")

page1 got master page so i use ContentPlaceHolder1 ,but still problem to get control id.

If i call mySidenav from page1, no error, but if i call from page2 which is inside iframe, cant call,it show error "
0x800a138f - JavaScript runtime error: Unable to get property 'ajaxRequestWithTarget' of undefined or null reference
"
Posted
Updated 12-Jun-20 1:47am
v2

1 solution

I'd attach a data attribute to the button that will allow you to hook onto it. So page1

HTML
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Page 1</h2>
    <p>
        <asp:Button ID="mySidenav" data-button="mySidenav" runat="server" />
    </p>
    <iframe src="Page2.aspx" />
</asp:Content>


page 2

HTML
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Page 2</h2>
    <script>
        var el = parent.document.querySelector("[data-button='mySidenav']");
        alert(el.id);
    </script>
</asp:Content>
 
Share this answer
 
Comments
SegunOk 3-Jun-21 12:02pm    
Thanks a million. This solution worked perfectly form me. I'm grateful.

SegunOk

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900