通过javascript更改iframe src时,整个页面会刷新

时间:2022-01-30 15:13:23

in my asp.net application I have a page That has two iframes. Above these frames are a few link buttons. When these buttons are hit, they should change the src of the two iframes. Listed below is the client side code and javascript I am using to do this. The issue is when any of these buttons are hit, instead of just changing the src of one of the iframes, the whole page refreshes. (i can tell this is refreshing because the link buttons are not available till you log in on the first page shown in the main frame, after you log in the buttons become visible via a call to the parent - window.parent.loginSuccess() ). If one of them is then clicked, it sends you back to the log in page in the iframe and the buttons are are no longer visible.) Here is the code, does anyone see why this is happening? Thank you for your help.

在我的asp.net应用程序中,我有一个页面,有两个iframe。在这些框架上方是一些链接按钮。当这些按钮被击中时,它们应该改变两个iframe的src。下面列出的是我用来执行此操作的客户端代码和javascript。问题是当任何这些按钮被击中时,整个页面刷新,而不是仅仅更改其中一个iframe的src。 (我可以说这是令人耳目一新的,因为直到您登录主框架中显示的第一页时,链接按钮才可用,登录按钮后,通过调用父窗口变得可见 - window.parent.loginSuccess() )。如果然后单击其中一个,它会将您发送回iframe中的登录页面,并且按钮不再可见。)以下是代码,是否有人知道为什么会发生这种情况?感谢您的帮助。

<body id="bodysty">
<div id="fstyle" class="daform">
    <form id="form1" runat="server">
    <div id="master">
    <div id="topthing"> <asp:LinkButton ID="lbtnControlPanel" onclientclick="CPhit()" runat="server" style="display:none">Control Panel</asp:LinkButton> <asp:LinkButton ID="lbtnAddClient" runat="server" onclientclick="AddClientHit()" style="display:none">Add Client</asp:LinkButton> <asp:LinkButton ID="lbtnManageClients" runat="server" onclientclick="ManageClientsHit()" style="display:none">Manage Clients</asp:LinkButton> <asp:LinkButton ID="lbtnAdmin" runat="server" onclientclick="ManageClientsHit()" style="display:none">Admin Options</asp:LinkButton></div>
    <div id="leftcontrol">
    <iframe id="LeftIframe" runat="server" ></iframe>
        </div>
    <div id="mainbodyform">

    <iframe id="ContentIframe" runat="server" src="Login.aspx" class="mainiframe" ></iframe>
    </div>
    </div>
    </form>
    </div>
</body>
</html>
<script language="javascript">
    function CPhit() {
        document.getElementById("ContentIframe").src = "LoginNotes.aspx";
    }
    function AddClientHit() {
        document.getElementById("ContentIframe").src = "NewCustomer.aspx";
    }
    function loginSuccess() {
        document.getElementById('lbtnControlPanel').style.display = 'inherit'
        document.getElementById('lbtnAddClient').style.display = 'inherit'
        document.getElementById('lbtnManageClients').style.display = 'inherit'
        document.getElementById('lbtnAdmin').style.display = 'inherit'
    }
</script>

There is no code in the codebehind that deals with this page, its simply for navigation. Any ideas? Thank you for your help.

代码隐藏中没有处理此页面的代码,仅用于导航。有任何想法吗?感谢您的帮助。

2 个解决方案

#1


2  

You need to cancel the default click action of the item you are clicking on.

您需要取消您单击的项目的默认单击操作。

Simple way is to return false on the click event.

简单的方法是在click事件上返回false。

onclientclick="CPhit(); return false;" 

better way is preventDefault()

更好的方法是preventDefault()

#2


0  

It might be related to the behaviour of the button Try

它可能与按钮Try的行为有关

event.stopPropagation() or  window.event.cancelBubble = true 

in your JS functions

在你的JS函数中

#1


2  

You need to cancel the default click action of the item you are clicking on.

您需要取消您单击的项目的默认单击操作。

Simple way is to return false on the click event.

简单的方法是在click事件上返回false。

onclientclick="CPhit(); return false;" 

better way is preventDefault()

更好的方法是preventDefault()

#2


0  

It might be related to the behaviour of the button Try

它可能与按钮Try的行为有关

event.stopPropagation() or  window.event.cancelBubble = true 

in your JS functions

在你的JS函数中