Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've used ajax to call web method and within that web method also called static method to generate dynamic script. Right at this moment it's required to write in
control which is required to access the control to do so from that static method. But can't access the control. Please note that I'm also using master page. Can anyone help me to identify the solution to access the
control from static method in code behind.

What I have tried:

I've used ajax to call web method and within that web method also called static method to generate dynamic script. Right at this moment it's required to write in <div> control which is required to access the control to do so from that static method. But can't access the control. Please note that I'm also using master page. Can anyone help me to identify the solution to access the <div> control from static method in code behind.
Posted
Comments
F-ES Sitecore 7-Oct-18 9:48am    
Your server-side controls are only available for use during a postback. When using ajax the only thing available to your server code is what you pass in the ajax method, and similarly the only response you can give is to pass data back to the ajax method, you can't update controls from your ajax method.

To update the page you'll need to do it via javascript when the ajax method completes. The webmethod will need to return the new label or text or whatever and your js needs to update the page as appropriate when the method returns.
Alamgir Hossan 9-Oct-18 13:50pm    
Thanks for your response. Yes, I've revisited the code and based on your comments, I've returned the dynamic content as well as JavaScript references from web method. I've tried to write this content using JavaScript and found that web method return value is correct. Also JavaScript did it's job and checked through alert and found ok what is required for me. But in web interface it's not reflecting the result. When I directly written the HTML code whatever written through JavaScript, it's giving the result in web interface.

Following is the Ajax Method and JavaScript Code for your review -

function AddDivContent(QCode) {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "DashboardSurveyDynamic.aspx/GenerateDivControl",
data: "{'QCode':'" + QCode + "'}",
success: function (success) {
var ltrContent = document.getElementById('PageBody_divContent');
alert("A");
ltrContent.innerHTML = JSON.stringify(success.d);
alert(ltrContent.innerHTML);
},
error: function (xhr, textStatus, error) {
alert("Error: " + error);
}

});
}

function AddScriptContent(QCode) {
alert("B");
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "DashboardSurveyDynamic.aspx/GenerateDashboardScript",
data: "{'QuesCode':'" + QCode + "'}",
success: function (success) {
alert("C");
var scriptContent = document.getElementById('PageBody_divScript');
scriptContent.innerHTML = JSON.stringify(success.d);
},
error: function (xhr, textStatus, error) {
alert("Error: " + error);
}
});

When I tried to retrieve the written content through view page source from browser, no code found in respected area where I wanted to write the HTML code. Following is the HTML code for your review -

//

AddDivContent Ajax will write the HTML Code Here which has been return from web method

//


//
AddScriptContent Ajax will write the code return from web method
//


Let me know, whether I'm missing anything or not and How can I resolve this issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900