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

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

5 Answers 1003 Views
Window
This is a migrated thread and some comments may be shown as answers.
Masterdom5
Top achievements
Rank 1
Masterdom5 asked on 12 Oct 2011, 02:45 PM
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
  

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Oct 2011, 03:07 PM
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.
0
Masterdom5
Top achievements
Rank 1
answered on 12 Oct 2011, 08:12 PM
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

0
Shinu
Top achievements
Rank 2
answered on 13 Oct 2011, 05:06 AM
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.
0
Masterdom5
Top achievements
Rank 1
answered on 01 Nov 2011, 06:46 PM
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



 

 

 

0
Marin Bratanov
Telerik team
answered on 03 Nov 2011, 10:49 AM
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
Tags
Window
Asked by
Masterdom5
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Masterdom5
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or