Click here to Skip to main content
12,078,070 members (47,372 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Ajax jQuery
Hi,
i have an ASP Page that contains these elements:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="js/page/jquery-1.8.3.js"></script>
    <script type="text/javascript">
 
        function Call() {
            $.ajax({
                type: 'POST',
                url: 'Default3.aspx/MyFuction',
                data: { name: 'Rasool' },
                success: function (data) {
                    alert(data);
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table border="1" align="center">
        <tr>
            <td>
                <input type="button" name="name" value="Click Me" onclick="Call();" />
            <td>
                <asp:TextBox runat="server" ID="txt" /></td>
        </tr>
    </table>
    </form>
</body>
</html>

and this is the CodeBhinde of my ASP Page,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected string MyFunction(string name)
    {
        return "this is your name : " + name;
    }
}

i want to send the Parameter 'name' to MyFunction in CodeBehind, and then return a string value to the Call function (that is a jquery.ajax function), and then the Call function must show the returned value from MyFunction in txt (textbox).

How can I do that?
Posted 8-Jan-13 22:43pm
Edited 8-Jan-13 22:44pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

//Add WebMtehod Attribute To Your Server Side Method


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    [WebMethod]
    protected string MyFunction(string name)
    {
        return "this is your name : " + name;
    }
}
  Permalink  
Comments
shajarian_lover 9-Jan-13 3:57am
   
I've add the [WebMethod], but it didn't work
this is result of Click on btn

http://http://www.cnetiran.com/capture.jpg
shajarian_lover 9-Jan-13 8:35am
   
now i solved problem but i have another question that is:
is this way secure to use to save user datas into database or no?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160212.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100