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

TimAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
TimAuthor 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
TimAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimAuthor Commented:
Found the answer myself.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.