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

Getting a value back from ajaxRequest with OnResponseEnd

6 Answers 255 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 2
Jason asked on 30 Apr 2009, 09:49 AM
Hi,

I've really struggled with this for a couple of days now and have tried several approaches.  I'm trying to use a toolbar button to add a record to my sql database and then refresh the data shown in my RadGrid to display the new record and then open a RadWindow to edit the new record.

I've got a toolbar button that when clicked runs the following jscript :-
switch (button.get_value()) {  
    case "AddInstance":  
        var treeview = $find("<%=RadTreeView1.ClientID %>");  
        var treeNode = treeview.get_selectedNode();  
        if (treeNode == null) {  
            alert("Select an application first");  
            break;  
            };  
        var application_id = treeNode.get_value();  
        var arguements = "AddInstance," + application_id;  
        RadAjaxManager1.ajaxRequest(arguements);  
        break;  

This calls the server-side code which successfully creates the sql record :-

Protected

Sub RadAjaxManager1_OnAjaxRequest(ByVal sender As Object, ByVal e As Telerik.Web.UI.AjaxRequestEventArgs)

 

Dim splitArguements As Array = Split(e.Argument, ",")  
Dim strFunction As String = splitArguements(0)  
Dim intIndex As Integer = splitArguements(1)  
 
Select Case strFunction  
    Case "AddInstance" 
        Dim hidden_idx As HtmlInputHidden = FindControl("hidden_idx")  
 
        SqlDataSource2.InsertParameters("instance_name").DefaultValue = "New Instance - " & CStr(Now)  
        SqlDataSource2.InsertParameters("application_id").DefaultValue = intIndex  
        SqlDataSource2.InsertParameters("testing_environment_id").DefaultValue = 1  
        SqlDataSource2.Insert()  
 
        hidden_idx.Value = CStr(NewInstanceId)  
        RadGrid1.Rebind()  
        MsgBox(hidden_idx.Value)  
 
End Select 

End Sub
 
Private Sub SqlDataSource2_Inserted(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Inserted  
 
    NewInstanceId = e.Command.Parameters("@instance_id").Value.ToString  
 
End Sub 
 
 
 

The above code works and the RadGrid1 is refreshed by my RadAjaxManager showing the new record.  The hidden inputs value is set and the messagebox appears with the correct index of the new record.

The next bit of code is where I'm struggling.  Although the code behind has set the value of the Hidden_idx control  I just can't ssem to find this control with the client side code.  I just keep getting null  :-
<body style="border:50px;">  
     
    <form id="form1" runat="server">  
      
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
    <script type="text/javascript">  
 
    function RadAjaxManager1_OnResponseEnd(sender, args) {  
 
        var hidden_input = $find("<%=Hidden_idx.ClientID %>");  
        alert(hidden_input);  
        var idx = hidden_input.value  
        alert(idx);  
          
        window.radopen("InstanceDetails.aspx?instance_id=" + instance_id, "RadWindow1");  
 
    }  
 
   </script> 
   </telerik:RadCodeBlock> 
 
   <input type="hidden" id="Hidden_idx" name="Hidden_idx" runat="server" /> 
 

Can you spot where I'm going wrong?  Is there a better way of doing this?  I've tried setting the innerhtml of a div which I could find with getElementByID but the innerhtml was always empty when read client-side even though the server side had set and read the value.

Thanks in advance for any help with this.
Jase

6 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 05 May 2009, 11:43 AM
Hello Jason,

Have you tried accessing the DOM element of the hidden field on the client with document.getElementById instead of $find which is the shortcut for findComponent method? Namely:

  <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">     
    <script type="text/javascript">     
    
    function RadAjaxManager1_OnResponseEnd(sender, args) {     
    
        var hidden_input = document.getElementById("<%=hidden_idx.ClientID %>");     
        alert(hidden_input);     
        var idx = hidden_input.value     
        alert(idx);     
             
        window.radopen("InstanceDetails.aspx?instance_id=" + instance_id, "RadWindow1");     
    
    }     
    
   </script>    
   </telerik:RadCodeBlock>    
 

I also noticed that the casing of the first letter ('H') differs from that you use from the code-behind of the page and this may be related to the issue as well.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Jason
Top achievements
Rank 2
answered on 06 May 2009, 07:14 AM
Hi Sebastian,

Yes I had tried document.getElementById and this found the hidden input.  The alert proves this as it returns [object].  But I still can't get the value of the hidden input client side,  the alert returns an empty string. 

Is this because the hidden input is part of the form and the value isn't committed until a postback has taken place?

function RadAjaxManager1_OnResponseEnd(sender, args) {  
 
    var hidden_input = document.getElementById("<%=hidden_idx.ClientID %>");  
    alert(hidden_input);  
    var idx = hidden_input.value;  
    alert(idx);  
 
    window.radopen("InstanceDetails.aspx?instance_id=" + idx, "RadWindow1");  
 
}  
 

I really appreciate your help with this Sebastian.

Thanks
Jase
0
Accepted
Sebastian
Telerik team
answered on 06 May 2009, 08:26 AM

Hello Jason,

Can you please verify that the hidden input is added as an updated control to the RadAjaxManager setting which marks the manager as an initiator of the request? The OnResponseEnd client event is raised when the response is received from the server and the hidden field should contain the updated value at that time.

Best regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Jason
Top achievements
Rank 2
answered on 06 May 2009, 02:12 PM
Wahooo !!  That was the answer.  Getting to grips with ASP.Net, Javascript and Ajax is a steep learning curve for me but you guys at Telerick are a great help!

Thanks
Jase
0
mfmz
Top achievements
Rank 1
answered on 03 Jun 2009, 07:40 AM
I have the same problem but different scenario. I get proper value from server side to client side on Ajax call but If I change value on client side, On Ajax call on server side hidden control value is empty. How I send client side value to server side on Ajax call.
0
Jason
Top achievements
Rank 2
answered on 05 Jun 2009, 01:33 PM
Hi,

You can simply pass the hidden fields value as an arguement to the ajaxmanagers postback request.  Here's what I did in my code.

var arguements = "AddInstance," + application_id;     
RadAjaxManager1.ajaxRequest(arguements);    
 

You can set the the arguements to pretty much what ever you like.  In your case you would $find the hidden field control and passback its value.  You can alert on this before calling the ajaxrequest just to make sure you've got what you need.

Hope that helps

Jase
Tags
Ajax
Asked by
Jason
Top achievements
Rank 2
Answers by
Sebastian
Telerik team
Jason
Top achievements
Rank 2
mfmz
Top achievements
Rank 1
Share this question
or