简体   繁体   English

如何<a>在iframe内部的</a> html <a>click事件</a>上调用服务器端函数<a>?</a>

[英]How to call a server side function on a html <a> click event from inside the Iframe?

I want to call a onclick function of server side like below(ie in test.aspx.cs ) 我想像下面那样调用服务器端的onclick函数(即在test.aspx.cs中

protected void linkTest_Click(object sender, EventArgs e)
{
    string a=HiddenField1.value;
    Response.Write("<script>alert("+a +" is called)</script>");
}

And i am passing the test.html as src in Iframe (ie in test.aspx ) 我在iframe中将src作为test.html通过(即在test.aspx中

<asp:HiddenField ID="HiddenField1" runat="server" />
<iframe id="Iframe" src="../test.html" ></iframe>

My HTML control is like below and it is inside test.html 我的HTML控件如下所示,它位于 test.html中

<a id="linkTest" runat="server" onclick="linkTest_Click">Test</a> 

Now,I want to call a server side onClick event( linkTest_Click ) on this <a> which is inside iframe control which is inside test.aspx 现在,我要在此<a>上调用服务器端onClick事件( linkTest_Click ),该事件位于test.aspx内的iframe控件内

You cannot call a server side function directly. 您不能直接调用服务器端函数。 But with some simple jQuery it is possible. 但是使用一些简单的jQuery就有可能。

On the page containing the iframe place the following code 在包含iframe的页面上,放置以下代码

<iframe src="index.html" width="200" height="100" frameborder="1"></iframe>

<asp:Label ID="Label1" runat="server"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" OnClick="Button1_Click" />

<script type="text/javascript">
    function executeIframeCode() {
        $('#Button1').click();
    }
</script>

Then in the iframe put a button or whatever you want and call the function executeIframeCode in the parent. 然后在iframe中放一个按钮或您想要的任何东西,并在父级中调用函数executeIframeCode

<button onclick="parent.executeIframeCode()" type="button">
    Execute Parent Code
</button>

Parent code behind code 父代码背后的代码

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Called from Iframe!";
}

This only works if the iframe page is on the same domain. 仅当iframe页面位于同一域中时,此方法才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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