Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 23rd, 2008, 12:38 AM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old June 23rd, 2008, 02:34 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old June 23rd, 2008, 02:38 AM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old June 23rd, 2008, 04:13 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
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.

Reply With Quote
  #5 (permalink)  
Old June 24th, 2008, 01:53 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:28 PM.


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