javascript $find return null

2 posts, 0 answers
  1. Gustavo
    Gustavo avatar
    23 posts
    Member since:
    Apr 2012

    Posted 19 Dec 2012 Link to this post

    Hi Telerik Team, I have this webpart:

    protected override void CreateChildControls()
    {
          ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
          if (scriptManager == null)
          {
            scriptManager = new RadScriptManager();
            this.Page.Form.Controls.AddAt(0, scriptManager);
          }
    }

    protected override void OnLoad(EventArgs e)
    {
          base.OnLoad(e);

          Panel panel = new Panel();
          panel.ID = "Panel1";
          this.Controls.Add(panel);   

          RadButton button = new RadButton();
          button.Height = 20;
          button.Width = 100;
          button.Text = "Click to update";
          button.ID = "ConfigureButton";
          button.OnClientClicked = "configureGauge";
          button.AutoPostBack = false;
          panel.Controls.Add(button); 

          radNumTextBox = new RadNumericTextBox();
          radNumTextBox.Height = 20;
          radNumTextBox.Width = 100;
          radNumTextBox.ID = "NewValueNumericBox";
          radNumTextBox.AllowOutOfRangeAutoCorrect = true;
          panel.Controls.Add(radNumTextBox);

          script = new Literal();
          script.ID = "scriptLiteral";
          script.Text = "<script type=\"text/javascript\">";
          script.Text += "function configureGauge() {";
          script.Text += "var value = $find('<%=NewValueNumericBox.ClientID %>');";
          script.Text += "alert(value);  }";
          script.Text += "</script>";
          this.Controls.Add(script);
    }

    When the configureGauge() function is called it returns null.
    Do you know why this is happening?

    Thanks,
    Gustavo
  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 20 Dec 2012 Link to this post

    Hello,

    You need to register the scripts through the ScriptManager, calling its RegisterStartupScripts() method and passing the script there.

    Regards,
    Iana Tsolova
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

Back to Top