document.getElementById(…)’ is null or not an object

6 posts, 0 answers
  1. Masterdom5
    Masterdom5 avatar
    4 posts
    Member since:
    May 2008

    Posted 12 Oct 2011 Link to this post

    When I try to retrieve the ClientID of radButton control, I got the Id of the control rather than the .NET format

    ctl00_....._btnSave and the getElementById returns a null value. When I do the same thing in a non RadWindow, everything works fine. Please help me solve this issue.


    The rad popup window:

     

     

       function ShowInsertForm(url, width, height, idName, idValue) {
        var extUrl = [url, "?" + idName + "=", idValue];
        var oWnd = window.radopen(extUrl.join(""));
        oWnd.setSize(width, height);
        oWnd.set_modal(true);
        oWnd.set_visibleStatusbar(false);
        oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close);

        return false;
       }


    This popup window has a Save and a Cancel buttons. When the user makes changes to the form and clicks the Cancel button, I need to display a javascript message to allow the user to confirm whether s/he wants to save the changes. if the answer is "YES", I want to execute the click event of the Save button. Here how I proceed:

    In the code behind:

    Protected Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
          If _presenter.IsDataChanged(Me.GetUserData()) Then

            InjectScript.Text = "<script type='text/javascript'>askquestion('" & btnSave.ClientID & "')</script>"
          Else
            InjectScript.Text = "<script type='text/javascript'>CancelEdit()</script>"
          End If
    End Sub

    In the Aspx code:
    function askquestion() {
          if (confirm("Data has changed. Do you want to save the changes?")) {
              document.getElementById('<%=btnSave.ClientID %>').click();
          }
          else {
              CancelEdit();
          }

          return false;
      }


    Thank you in advance.

    Masterdom5
      

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Oct 2011 Link to this post

    Hello Masterdom,

    When you are trying to access RadControls the correct method is to use $find instead of document.getElementByID.

    var btn = $find("<%=btnSave.ClientID %>");

    Thanks,
    Shinu.

  3. Masterdom5
    Masterdom5 avatar
    4 posts
    Member since:
    May 2008

    Posted 12 Oct 2011 Link to this post

    Hi Shinu,

    Thank you for your quick reply. As suggested, I replaced the getElementById function by $find and I received another error which is "null is null or not an object".

    here's my code:

    JS:
    function askquestion() {
          if (confirm("Data has changed. Do you want to save the changes?")) {
              var button = $find("<%= btnSave.ClientID %>");
              button.click();
          }
          else {
              CancelEdit();
          }
      }


    ASPX:
         <telerik:RadButton runat="server" ID="btnSave" Text="Save" meta:ResourceKey="btnSaveResource1" />

    Code Behind:
        Protected Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
          Dim myString As String = "<script type='text/javascript'>askquestion()</script>"
          ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "askquestion", myString, True)
    End Sub

    The affected page is a Rad popup window and the update panel is declared in the master page.

    Thank you in advance.

    Masterdom5

  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 13 Oct 2011 Link to this post

    Hello MasterDom,

    I am not sure about the issue that you are facing. Please make sure that the JS code is called before the controls are fully loaded on the page One suggestion is either call the JavaScript code with a small timeout, or to use the Sys.Application.Load event and call the code there.
    Calling radalert from codebehind (all versions of RadWindow).

    Thanks,
    Shinu.

  5. Masterdom5
    Masterdom5 avatar
    4 posts
    Member since:
    May 2008

    Posted 01 Nov 2011 Link to this post

    Hi,

    Thank you for your reply. I tried to resolve the issue but no still no go. Now I have a different result. The following javascript statement in a telerik popup window returns javascript:void(0).

    function AskQuestion() {
      var button = getElementById("<%= btnSave.ClientID %>");
    }

    btnSave is declared as follow:
         <telerik:RadButton runat="server" ID="btnSave" EnableBrowserButtonStyle="true" Text="Save" meta:ResourceKey="btnSaveResource1" />

    Calling the JS function:

        Protected Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
          Dim myString As String = "AskQuestion()"
          ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "AskQuestion", myString, True)
          Dim str As String = "<script type='text/javascript'>AskQuestion()</script>"

          If _presenter.IsDataChanged(Me.GetUserData()) Then
            If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
              ClientScript.RegisterStartupScript(Me.GetType(), "clientScript", str)
            End If
          Else
            InjectScript.Text = "<script type='text/javascript'>CancelEdit()</script>"
          End If

        End Sub

    Any assistance will be very much appreciated.

    Thank you.

    Masterdom5



     

     

     

  6. Marin Bratanov
    Admin
    Marin Bratanov avatar
    2675 posts
    Member since:
    Jun 2013

    Posted 03 Nov 2011 Link to this post

    Hi  Lucner,

    Please examine this help article on executing a JavaScript function from the server: http://www.telerik.com/help/aspnet-ajax/radwindow-troubleshooting-javascript-from-server-side.html.

    Note that AJAX enabled controls are loaded on the page after the Sys.Application.Load event while the script is executed early, when the page is loaded (around the window.onload event), so you should utilize the former.


    Regards,
    Marin
    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