This is a migrated thread and some comments may be shown as answers.

Input control reference is null when called from window.onload()

1 Answer 158 Views
Input
This is a migrated thread and some comments may be shown as answers.
SultanOfSuede
Top achievements
Rank 1
SultanOfSuede asked on 13 Feb 2009, 06:14 PM
I have this JavaScript method (setExpansionControls) which is called from window.onload().  The call works just fine.  The variable txtBox refers to a RadNumercInput control.


            function setExpansionControls(enable) {
                var txtBox = $find('ctl00_ContentPlaceHolder1_RMExpandableInSqFt');

                if (txtBox != null) {
                    if (enable) {
                        txtBox.enable();
                    }
                    else {
                        txtBox.disable();
                    }
                }
            }

The problem: txtBox is ALWAYS null when called from onload, but it is not when called from elsewhere.  I can disable/enable the input control just fine if I click a button on a loaded page.

So my question:  How do we know when we can safely call methods on an initialized input control?  If window.onload isn't able to get a reference to the control, what can?

TIA,

Ian

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Feb 2009, 05:42 AM
Hello,

In ASP.NET AJAX environment, the ASP.NET AJAX controls (including RadControls for ASP.NET AJAX) are created after the page has been loaded. So the client side object of RadControls getting as null. You can create function that use the reserved name pageLoad (which fires when page loads) and inside that you can access the client side object for each control or call the function that you want. See the example below.

JavaScript:
<script type="text/javascript">  
var enable=false;  
function pageLoad()  
{  
    setExpansionControls();  
}  
function setExpansionControls()  
{  
    var txtBox = $find("<%= RadNumericTextBox1.ClientID %>");  
    if (txtBox != null)   
    {  
        if (enable)  
        {  
            txtBox.enable();  
        }  
        else   
        {  
            txtBox.disable();  
        }  
    }  
}  
</script> 

Thanks,
Princy.
Tags
Input
Asked by
SultanOfSuede
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or