Click here to Skip to main content
15,918,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have an ASP Page that contains these elements:

ASP.NET
<%@ 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,
C#
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
Updated 8-Jan-13 21:44pm
v2

1 solution

//Add WebMtehod Attribute To Your Server Side Method


C#
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;
    }
}
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900