简体   繁体   中英

How to communicate between two aspx pages using js?

I have two aspx pages namely one.aspx and two.aspx and i have a javascript file named link.js

Now i need to communicate between this two aspx pages using javascript file as intermediate.

In one.aspx file i am having an ModalPopupExtender and the content of its panel is loacted in two.aspx ie, in "one.aspx" :

<html>
<head>    
<script type="text/javascript" src="link.js"></script>

<script type="type="text/javascript">
   function test(){
     var str=callPopup();
     document.getElementById('pnlPopUp').innerHtml = str;
   }
</script>
</head>
<body>
<ajaxToolkit:ModalPopupExtender ID="popup1" runat="server" TargetControlID="btnOK" CancelControlID="btnCancel" PopupControlID="pnlPopUp"></ajaxToolkit:ModalPopupExtender>

<asp:Panel ID="pnlPopUp" runat="server"></asp:Panel>

  </body> 
</html>

in "two.aspx" :

<html>
  <head>
    <script type="text/javascript" src="link.js"></script>
   </head>
  <body>
<asp:Panel ID="pPanel1" runat="server">
    <table>
        <tr>
            <td>
                <p>test</p>
            </td>
            <td>
                <asp:Button ID="BtnTest" runat="server" Text="Click" OnClientClick="javascript:alert('hello world..!'); return false;" />
            </td>
        </tr>
    </table>
</asp:Panel> </body> </html>

in link.js:

function callPopup() {
    var s = document.getElementById('pPanel1').innerHtml;
    return s;
}

i am able to call callPopup() from one.aspx but unable to get the content from two.aspx page. please help me.

Thanks in advance.

I'll give you 2 ways to accomplish this.

  1. Use SignalR to broadcast to multiple clients. Read more at https://github.com/SignalR/SignalR

  2. (more low level. SignalR usually takes care of all of this for you)

Try using Ajax to call a function server side: http://www.w3schools.com/ajax/default.asp . You can then have the function that is called server side write to some output source; Some examples: file, session, cookie.

Then have the other web page constantly reading from that same output (the file, session or cookie) source via Ajax.

Are you sure that your architecture is correct? Is there really a good reason why you need two.aspx - can't one.aspx do it all?

Having used the AjaxToolkit in the past, I struggle to think of a good reason to use it in modern web development. Have you looked at http://knockoutjs.com/ or Backbone ? They are really worth a look.

I know this doesn't answer your question per se , I'm just trying to save you from a world of pain.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM