kavitha vemula kavitha vemula - 4 months ago 32
Ajax Question

passing webmethod parameter to another method

below is my code which return name that is entered in textbox in the web method and displays the name current time and date as output now what i need is i only need to pass the name to another method how can i do this

<title></title>

<script type="text/javascript">
function ShowCurrentTime() {
PageMethods.GetCurrentTime(document.getElementById("<%=txtUserName.ClientID%>").value, OnSuccess);
}
function OnSuccess(response, userContext, methodName) {
alert(response);
}
</script>

</head>
<body>
<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()"/>
</div>
</form>
</body>

protected void Page_Load(object sender, EventArgs e)
{

}

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}


now what i need is i need another method only with name that is obtained from webmethod and on button click i need to display the name with method name how can i do this

Answer

There can be a few corrections first of all you need document.ready event or another helper function (i changed to a helper function due to asp.net's clientid thing) and then you have to stop button posting back your page.

script type="text/javascript">
    var onSubmitClick = function() {
            var button = this;
            var obj = {};
            obj.name = $.trim($("[id*=txtName]").val());
            obj.age = $.trim($("[id*=txtAge]").val());
            $.ajax({
                type: "POST",
                url: "CS.aspx/SendParameters",
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert(r.d);
                }
            });
            return false;
    };
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>

<table border="0" cellpadding="0" cellspacing="0">
<tr>
    <td>
        Name:
    </td>
    <td>
        <asp:TextBox ID="txtName" runat="server" Text = "" />
    </td>
</tr>
<tr>
    <td>
        Age:
    </td>
    <td>
        <asp:TextBox ID="txtAge" runat="server" Text = ""/>
    </td>
</tr>
<tr>
    <td>
        <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClientClick="return onSubmitClick();" UseSubmitBehaviour="false" />
    </td>
</tr>
</table>

protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod]
        public static string SendParameters(string name, int age)
        {
            return string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine);
        }
Comments