0

i am calling two server side function with the help of page method but nothing happen. here i am giving my code. so please tell me what mistake i made,

my aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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>

</head>
<body>
    <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
    <a href="#" onclick="javascript:preview();">Test</a>
        <br />
    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

     <br />
     <br />
     <asp:Button ID="btnSetvwState" runat="server" Text="Set Session Val" OnClientClick="SetSessionVal;return false;" />
        <asp:Button ID="btnGetVwState" runat="server" Text="Get Session Val" OnClientClick="GetSessionVal;return false;"/>
   <script language="javascript">
       function preview() {
           alert($get('TextBox1').value);
           PageMethods.GetMessage("Hi", "Joy", onSuccess)
           return false;
       }

       function onSuccess(res) {
           alert(res);
       }

       function GetSessionVal() {
           PageMethods.GetViewState(onSuccess)
           return false;

       }

       function SetSessionVal() {
           alert("pp");
           PageMethods.SetViewState("Hi", "Tridip", onSuccess)
           return false;

       }
    </script>
    </form>
</body>
</html>

my server side code


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

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod]
        public static string GetMessage(string pr1,string pr2)
        {
            return pr1 + " " + pr2;
        }

        [System.Web.Services.WebMethod(EnableSession=true)] 
        public static void SetViewState()
        {
            HttpContext.Current.Session["data"] = "Hellp";

        }

        [System.Web.Services.WebMethod(EnableSession = true)]
        public static string GetViewState()
        {
           return (String) HttpContext.Current.Session["data"] ;

        }
    }
}

please help me to catch the mistake. thanks

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 3
    what are you seeing? What doesn't work, what do you see? – Russ Cam Apr 19 '11 at 19:42
  • Have you tried to debug this? Have you set breakpoints? What happened? We're not going to debug your code for you, but we'll help you if you'll say what you've tried. – John Saunders Apr 19 '11 at 19:47
  • try `OnClientClick="SetSessionVal();return false;"` and keep in mind, the return false can cause the server side click event handler not to fire. – Bala R Apr 19 '11 at 19:51

1 Answers1

2

missing () here (SetSessionVal, GetSessionVal):

 <asp:Button ID="btnSetvwState" 
             runat="server" Text="Set Session Val" 
             OnClientClick="SetSessionVal();return false;" />
    <asp:Button ID="btnGetVwState" 
                runat="server" Text="Get Session Val" 
                OnClientClick="GetSessionVal();return false;"/>
manji
  • 47,442
  • 5
  • 96
  • 103
  • can't say for sure that this is causing his issue, but it looked weird to me as well. – Al W Apr 19 '11 at 19:51
  • yes there was missing () but when i use OnClientClick="SetSessionVal();return false;" then also pagemethod is not being called. i am unable to catch the error. so please tell me what is missing. – Thomas Apr 20 '11 at 08:21