Can't reference radbutton in javascript from server side

4 posts, 0 answers
  1. Stanley
    Stanley avatar
    13 posts
    Member since:
    Jun 2011

    Posted 30 Aug 2011 Link to this post

    Hi all,

    This does not work when I call this from my server side code.

    I get an error :'null is null or not an object.'

    It seems that the documentation rarely mentions how to reference jscript from server side code and there are not enough examples to demonstrate this.

    According to your documents I used:

    var button = $find("<%= RadbuttonID.ClientID %>");



    My code from the server is called onindexchanged of my combobox:

    public void RadComboBox4_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
            {
     
                ScriptManager.RegisterStartupScript(this, this.GetType(), "fireClientEvent", "changeButtonState()", true);
                            
     
            }

    My javascript and ASPX code is:

    function changeButtonState()
           {
              //alert('test popup');
               var button = $find("<%= btnSelectDeal.ClientID %>");
               alert(button.get_text());
            
          }

    <telerik:RadButton runat="server" Text="Select" ID="btnSelectDeal"
                     UseSubmitBehavior="False"  Skin="Telerik"
                     OnClick="btnSelectDeal_Click" onclientclicked="OnClientClicked" >
                </telerik:RadButton>

    I can get the first alert in my jscript to fire but not the second one. Any ideas? I have this script in my code in my RadScriptBlock.


    Thanks
  2. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 30 Aug 2011 Link to this post

    Hello Stanley,

    As with all ajax controls, they first need to create their client object before they can be used, which is why your code throws an error. You need to change your code like so:

    public void RadComboBox4_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
            {
       
                ScriptManager.RegisterStartupScript(this, this.GetType(), "fireClientEvent", "Sys.Application.add_load(changeButtonState)", true);                        
       
            }

    This way the RadButton client object is created and you now reference it.

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stanley
    Stanley avatar
    13 posts
    Member since:
    Jun 2011

    Posted 31 Aug 2011 Link to this post

    Thanks for this Kevin. It now works.

    According the documentation I was told that if I used ScriptManager then there is no need to use ""Sys.Application.add_load" ?

    "If you are working with Ajax callbacks however, I would suggest to use asp:Scriptmanager’sRegisterStartupScript() method. The good thing in such scenario is that the control (being an ASP.NET AJAX control) will first be rendered on the page along with all other ASP.NET AJAX controls and then will execute the JavaScript code, i.e., there will be no need to use the Sys.Application.add_load() approach."
  5. Svetlina Anati
    Admin
    Svetlina Anati avatar
    2795 posts

    Posted 01 Sep 2011 Link to this post

    Hello Stanley,

     We apologize if the information was misleading for you but the text you have taken is a part of our resources which is for another case and it is correct in that context. The whole context of that information is as follows:


    If you are working with Ajax callbacks however, I would suggest to use asp:Scriptmanager’s RegisterStartupScript() method. The good thing in such scenario is that the control (being an ASP.NET AJAX control) will first be rendered on the page along with all other ASP.NET AJAX controls and then will execute the JavaScript code, i.e., there will be no need to use the Sys.Application.add_load() approach.
     
    ASPX
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager
        <telerik:RadWindowManager ID="RadWindowManager1" runat="server"
        </telerik:RadWindowManager
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"
            <ContentTemplate
                <asp:Button ID="Button1" Text="CallBack and show RadAlert" runat="server" OnClick="Button1_Click" />
            </ContentTemplate
        </asp:UpdatePanel
    </form>
     
    C#
    protected void Button1_Click(object sender, EventArgs e) 
        string scriptstring = "radalert('Welcome to Rad<strong>Window</strong>!', 330, 210);"; 
        ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", scriptstring, true); 
    }
     
    VB.NET
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
        Dim scriptstring As String = "radalert('Welcome to Rad<strong>Window</strong>!', 330, 210);"
        ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "radalert", scriptstring, True) 
    End Sub 

    In this case as you see the RadControl (the RadWindowManager in this case) is outside the update panel - this means that its client object does not get disposed when the button is clicked. If it is inside as in your case, the whole control will be disposed and recreated due to being updated with the AJAX request and in this case you would need to use the Sys.Application.load event. Note, that this is not directly related to RadControls only but this is how all ajax client objects work.

    I hope that the provided explanation is detailed enough and helpful and in case you have any additional questions do not hesitate to contact us again.


    Kind regards,
    Svetlina
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top