Saravanan Ponnusamy

Saravanan Ponnusamy

  • 358
  • 4.4k
  • 1.1m

ASP.Net AJAX-- how to display ajax response in html

May 10 2018 8:37 AM
my code:
  1. $(document).ready(function () {  
  2. $("[id*=Aspx_Dropdown_Back]").change(function () {  
  3. $.ajax({  
  4. type: "POST",  
  5. url: "View.aspx/GetVariables",  
  6. data:JSON.stringify({"xszBack": document.getElementById ( "<%= Aspx_Dropdown_Back.ClientID %>" ).value}),  
  7. contentType: "application/json; charset=utf-8",  
  8. dataType: "json",  
  9. success: OnSuccess,  
  10. failure: function() {  
  11. alert("F");  
  12. }  
  13. });  
  14. });  
  15. });  
  16. function OnSuccess(data) {  
  17. var msg = data.hasOwnProperty("d") ? data.d : data;  
  18. $("#divToAddTo").empty();  
  19. $("#divToAddTo").html(msg);  
  20. }
code behind:
  1. [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]  
  2. public static string GetVariables(string xszBack)  
  3. {  
  4. string Retrunstring = "";  
  5. Retrunstring = "<asp:TextBox ID='TextBox31' runat='server' CssClass='TextBox_Text12' Width='250' />";  
  6. return zszRetrunstring;  
  7. }
im trying to get string(HTML string) from code behind function and display in DIV tag by using ajax reponse data.
 
but i cant do that.. no error raising..
 
if i given that HTML string directly in AJAX Success methos,, like below..
  1. function OnSuccess(data) {  
  2. var $msg = data.hasOwnProperty("d") ? data.d : data;  
  3. $("#divToAddTo").empty();  
  4. //$("#divToAddTo").html($msg);  
  5. $("#divToAddTo").html('<asp:TextBox ID="TextBox11" runat="server" CssClass="TextBox_Text12" Width="250" /><br/><asp:TextBox ID="TextBox21" runat="server" CssClass="TextBox_Text12" Width="250" /><br/>');  
  6. }  
its working fine... and two textbox's are generated inside DIV tag.
 
but the string, same string which i get from code behind.. its not working..
 
pls. dont refer any web reference... pls. give any code..
 
thanks all...

Answers (3)