I have an iFrame, which contains a RadButton. The ID is btnTest.

Now, I try to call the click-method of this RadButton from outside of the iFrame. I have tried:

function SaveClick() {
    var iframe = document.getElementById('ifr');
    alert(iframe.id);
    var doc = iframe.contentWindow.document;
    if(doc) {
        alert('aaa');
    }
    //var btn = doc.getElementById('<%=btnTest.ClientID %>');
    var btn = $find('<%=btnTest.ClientID %>', doc);
    if(!btn) {
        alert('bbb');
    }
    btn.click();
}

but nothing is working.

I can get the iFrame (the id of the iFrame is showing in the first alert) and also 'aaa' is showing in the second alert. But I cannot get the btn (the third alert with 'bbb' is also showing because of the not in the if).

Can someone help me please, how to get the control?

有帮助吗?

解决方案

Create a function in the page that you put in the iframe. This function will return a reference to the button.

var btn = iframe.contentWindow.getMyRadButton();
btn.click();

where getMyRadButton is something like

function getMyRadButton() {
 return $find("<%=RadButton1.ClientID%>");
}

其他提示

You use onclick, for example:

document.getElementById("ele").onclick = function(){
    alert("Hello World");
}

document.getElementById("ele").onclick();

And you should have a function made for that elemenet for onclick.

http://jsfiddle.net/3RTGL/1/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top