Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
|
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2008, 12:38 AM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Need Help with using Ajax and C#.

Hi

I am having a hard time trying to get ajax to work with asp.net and C# due to the lack of tutorials. I am NOT using the script manager nor do I want to. I know I could use the update panel and I could be done 100 times over by now but I don't learn too much from dragging in dropping a control in.

So right now I am starting simple. I want to have a button and when it is clicked I want it to submit a hard coded value and capture it in the C# and then print out a message.

So my button is a submit button(not an asp.net button) and I am using jquery.

So this is what I have

    $("#btnSave").click(function(event)
    {
        $.post("FlashCardQuiz.aspx.cs", "test");
    });

Now I don't know how to capture the "test" value in my C# and spit it back.

Thanks

 
Old June 23rd, 2008, 02:34 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm not that familiar with jquery's syntax but when you post data you normally send a name/value pair such as forename=joe or more than one pair, forename=joe&surname=fawcett. Then you read them server-side from the Request.Form collection, string forename = Request.Form["forename"].

How does the post method in your example work, what do the two arguments represent?

--

Joe (Microsoft MVP - XML)
 
Old June 23rd, 2008, 02:38 AM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Quote:
quote:Originally posted by joefawcett
 I'm not that familiar with jquery's syntax but when you post data you normally send a name/value pair such as forename=joe or more than one pair, forename=joe&surname=fawcett. Then you read them server-side from the Request.Form collection, string forename = Request.Form["forename"].

How does the post method in your example work, what do the two arguments represent?

--

Joe (Microsoft MVP - XML)
The first one is the url and the second on is the Key/value.

http://docs.jquery.com/Ajax/jQuery.p...tacallbacktype

 
Old June 23rd, 2008, 04:13 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

I made a test page with one button on it. All I want is when the button is pressed it goes to the server captures the value and spits it backout.

How can I do this?

Here is my code you can even run it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="jquery-1.2.6.js" type="text/javascript"></script>



<script type="text/javascript">

    $("#submit").click(function()
    {
        $.post("Default.aspx", {SendMe: "send"});
    });

</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="submit" value="submit" id="save" />
    </div>
    </form>
</body>
</html>


code behind


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.HttpMethod == "POST")
        {
            string text = Request.Form["SendMe"];
            Response.Output.WriteLine(text);
        }
    }
}

You just need to get jquery from the jquery site.

 
Old June 24th, 2008, 01:53 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That looks fine if the way you have formatted the post data as an object is correct. Also I would check if it's a postback
Code:
if (Page.IsPostBack && Request.HttpMethod == "POST")
although the
Code:
Request.HttpMethod == "POST"
part is probably superfluous.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Recommend an AJAX IDE - JoyiStar AJAX WebShop. kingstar Ajax 4 December 15th, 2006 05:12 AM
Need Help using AJAX s_saravananerd Ajax 0 October 16th, 2006 01:08 AM
New Ajax article: Creating an Ajax Search W jminatel BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 May 11th, 2006 03:45 PM
new Ajax article: Creating an Ajax Search Widget jminatel Ajax 0 May 11th, 2006 02:50 PM
New Ajax Article: Ajax Submission Throttling jminatel Ajax 0 April 11th, 2006 08:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.