$find not working

3 posts, 1 answers
  1. SSirica
    SSirica avatar
    116 posts
    Member since:
    Jan 2007

    Posted 20 Mar 2012 Link to this post

    I have created the simplest form I can to test setting a RadTextBox using javascript.

    <head runat="server">
    <title>Textbox Page Load Testing</title>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="SCRM">
    <telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" DecoratedControls="All"
    EnableRoundedCorners="True" />
    <telerik:RadTextBox ID="txtTest" runat="server" Skin="" TextMode="SingleLine">
    <telerik:RadCodeBlock runat="server" ID="RadScriptBlock1">
    <script type="text/javascript">
    function setVal(val) {
    $find("<%=txtTest.ClientID %>").set_value(val);

    Partial Class Default4
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            ClientScript.RegisterStartupScript(Me.GetType(), "Pass", "setVal('TestValue');", True)
        End Sub
    End Class

    I get the following error:
    Microsoft JScript runtime error: Unable to get value of the property 'set_value': object is null or undefined

    Yes I have to do it on page load.
    Yes I have to do this using javascript.

  2. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Mar 2012 Link to this post


    In ASP.NET AJAX environment, the ASP.NET AJAX controls (including RadControls for ASP.NET AJAX) are created after the page has been loaded. That's why you are not getting client side object of RadTextBox.
    All ASP.NET AJAX controls however (including ours), are registered on the page on a later stage - in Sys.Application.Load - you can easily verify that by putting ASP.NET AJAX controls on a page and examining the HTML dump once the page is loaded in the browser. Basically, what happens is that your code is outputted and executed on the page, before the Ajax controls are rendered, which causes the error. Try
    the following code to eliminate the error.
    protected void Page_Load(object sender, EventArgs e)
      string script = "<script language='javascript'>function f(){setVal('TestValue'); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Pass", script);

    Another approach is that you can add the client side object of RadTextBox when page loads(OnLoad) and inside that you can access the client side object for each control. Try the following approach.
    <telerik:RadTextBox ID="txtTest" runat="server" Skin=""  TextMode="SingleLine" ClientEvents-OnLoad="OnLoad">
    <script type="text/javascript">
     function OnLoad()
       $find("<%=txtTest.ClientID %>").set_value('TestValue');

  3. SSirica
    SSirica avatar
    116 posts
    Member since:
    Jan 2007

    Posted 21 Mar 2012 Link to this post

    That worked.  Thanks Princy.
Back to Top