1

I need help with my logic...

Currently:

  1. A user inputs some data in a page from.
  2. The user clicks submit.
  3. Javascript places their data in a hidden input field.
  4. The page does a postback (reloads)
  5. C# registers that this is a postback and gets the hidden javascript field.
  6. C# puts it in the database.

The problem with the above is the reloading of the webpage. I do not want that. I have played around with a callback script that is not working (I have another question open on trying to debug that).

My question is about alternatives to accomplish my steps as defined above without a callback or postback? Is there a way to simply call the server-side function on demand from a client side function? (I know callbacks should do this very thing, but mine is not working)

If there is no such alternative, why is it that callbacks are so complicated (from a novice's point of view)? Why can I not simply call a callback like:

if (IsCallBack)
        {
            string test = Request.Form["saveTest"];
            //do stuff
        }

Thank you. Also, please do not 'close' or mark down my question without first critiquing me. Thanks.

yoyo
  • 79
  • 3
  • 11
  • I've heard of this but it just seems overly complicated... – yoyo Jul 19 '13 at 17:38
  • when they told tebow he had to change his throw to go pro, did he say it was overly complicated?! – Jonesopolis Jul 19 '13 at 17:44
  • Unfortunately ASP.Net is not as seamless with AJAX as it could be (ASP.Net MVC is much better in that regard). – McGarnagle Jul 19 '13 at 18:00
  • if you arrive at the same url after the submit, you can use ajax to submit and simply inject the BODY of the response into document.body.innerHTML, much like rails4. i'd recommend jQuery's .html() becasue it firese script tags for you. if you redirect, you're looking at custom wiring the fields to HTML somehow. – dandavis Jul 19 '13 at 18:24
  • @Jonesy When they told Columbus the world was flat and thats the way it is... I'm just trying to think outside the box, but I see your point. – yoyo Jul 19 '13 at 18:25
  • @McGarnagle I am working with Ajax now (cant get that to work either). Could you give me some examples of ASP.Net MVC? – yoyo Jul 19 '13 at 18:26
  • @dandavis could you provide an example of this? I do not quite follow. My end goal is to put this data into a database and xml file. – yoyo Jul 19 '13 at 18:29

1 Answers1

1

Here's the simplest example I can think of, that uses Ajax with ASP.Net.

  • using JQuery, because it simplifies making the Ajax request
  • not using ASP.Net AJAX, because it adds abstraction on top of the core technology, and that is not a good way to start learning

First, UserInput.aspx

<html>
    <input type='text' id='SomeUserInput' />
    <input type='submit' value='submit' id='SubmitButton' />

    <script src='http://code.jquery.com/jquery-1.10.2.js'></script>
    <script>  
        // wait for the document to load
        $(document).ready(function() {

            // handle the button click
            $("#SubmitButton").on("click", function(e) {

                // stop the page from submitting, so we can make the Ajax call
                e.preventDefault();

                // retrieve the user input
                var userInput = $("#SomeUserInput").val();

                // make the Ajax request
                $.post("AjaxHandler.aspx", { saveText: userInput }, function(response) {

                    // handle response from the server
                    alert("Request is complete.  Result: " + response);
                });
            });
        });
    </script>
</html>

Second, AjaxHandler.aspx

<%@ Page Language="C#" %>

<%
    string userInput = Request.Params["saveText"];

    // use the input here

    Response.Write("done, maybe?");
%>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Thank you for your response. What can I use in place of AjaxHandler.aspx? I am trying out an example from [link](http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/) and the issue seems to be with the url: mypage/csharpfunction – yoyo Jul 19 '13 at 18:33
  • @matthewPetersUF ah, so you're using ASP.Net AJAX ... that's cool, but there are a few more steps involved to get it right. If you're having a specific problem, probably best to detail that in a new question. – McGarnagle Jul 19 '13 at 18:39
  • I was playing around with ajax to get it working, but although I still can't, I do not think that is the best approach anyway because I would have to create a public method and therefore expose a security risk. Are there any other ways to accomplish a callback or something like a callback? – yoyo Jul 19 '13 at 18:43
  • @matthewPetersUF no, the only other way is doing a full postback/page reload. It shouldn't be a bigger security risk than anything else -- AJAX calls are blocked by default unless they originate from the same site. – McGarnagle Jul 19 '13 at 18:47
  • Well thanks, I will mess around with AJAX a bit more (I am not too familiar with it). – yoyo Jul 19 '13 at 19:07