Search code examples
javascriptasp.netiframetelerik

Call click() of RadButton in JavaScript


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?


Solution

  • 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%>");
    }