AJAX call to C# page Method function returning whole web page.

I am calling my C# page Method function in my code behind file:

 xmlhttp.open("POST", "hdsearch.aspx/SearchHomeDrive", true);
 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xmlhttp.send("Method=" + encodeURIComponent("SearchHomeDrive") + "&SOEID=" + encodeURIComponent(strIDs) + "&GetHDSize=" + encodeURIComponent(getHDSize));

Open in new window


 This works fine, no issues there. The issue I have is when the function in C# ends and sends the Response back to my AJAX call it also sends the entire web page with it, so under the table I create (using the Response from C#) is my web page in mirrored form. I confirmed this by outputting the ResponseText from the AJAX call.

 The page code is being added to the Response after my C# code ends, or at least that is my assumption.

 Any ideas?

 Thanks

 
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack)
 {
 if (HttpContext.Current.Request.Form["Method"] == "SearchHomeDrive")
 {
 SearchHomeDrive(HttpContext.Current.Request.Form["SOEID"], HttpContext.Current.Request.Form["GetHDSize"]);
 }
 }
 }

 protected void SearchHomeDrive(String SOEID, String getHDSize)
 {
 HttpContext.Current.Response.Write("Test"); 
 }
 

Open in new window

tr57285Asked:
Who is Participating?
 
tr57285Author Commented:
I found my answer. I had to add a Response.End() to the end of my C# function and that keeps it from returning the whole page.
0
 
Julian HansenCommented:
Firstly, any reason you are not using something like JQuery for the AJAX call - might make your life a bit simpler.

If you do you could use the .load function which you can give a url with a #id and it will load only that ID into the specified element,

Alternatively, build your server side code to render the bit you want returned separately - and call / load this from your main page load

<div class="main-page-header-n-stuff">
...
</div>
<div class="main-content">
<%= mainContent();%>
</div>

(Above is illustration only) - then when you detect your AJAX call back simply do the mainContent() part
0
 
tr57285Author Commented:
Hey thanks for responding back. I actually did use Jquery and I had other weird issues with that so I ended up ditching it in favor of xmlhttprequest.

The issue here is probably my lack of understanding on how the page method process works because I had this same jscript code working fine in classic asp with a vbscript code behind file. Once I renamed to aspx and changed to a c# code behind this started happening.

From what I have seen when you call a page method it calls the page_load () function which in turn you use to call your main function.

The call to my main function seems to be an async call because I sent a text response immediately following the call to my main function and that test response was output before the response from my main function.

The addition of the Web page code to the response stream seems to be getting added after the page_load () flow is completed.
0
 
Julian HansenCommented:
Just a question - is there reason you are not using an update panel for this?
0
 
tr57285Author Commented:
Found the answer myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.