public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        }
        [System.Web.Services.WebMethod]
        public static void AddInfo()
        {
            twoManager tm = new twoManager();
            two t = new two();
            t.Username = HttpContext.Current.Request.QueryString["username"].ToString();
            t.Userpwd = HttpContext.Current.Request.QueryString["userpwd"].ToString();
            t.Age = HttpContext.Current.Request.QueryString["age"].ToString();
            t.Sex = HttpContext.Current.Request.QueryString["sex"].ToString();
            tm.AddInfo(t);
        }
    }

解决方案 »

  1.   

    <head id="Head1" runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
        function Add()
        {
        PageMethods.AddInfo();
        }
        </script>
    </head>
    <body>
        
        <form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <div>
        用户名<input type="text" name="username" id="uname" /><br />
        密码<input type="text" name="userpwd" id="upwd"/><br />
        年龄<input type="text" name="age" id="uage" /><br />
        性别<input type="text" name="sex" id="usex"/><br />
        <input type="button" name="Button" value="添加" onclick="Add()"/>
        </div>
        </form>
    </body>
    -----------------------------------
    运行后报错Microsoft JScript 运行时错误: Sys.Net.WebServiceFailedException: 服务器方法“AddInfo”失败,并发生以下错误: System.NullReferenceException-- 未将对象引用设置到对象的实例。高手指教
      

  2.   

    你的方法定义有问题吧,如果要前台ajax调用定义为ajaxmethod方法,你这样是 webservice方法,前台不能这样调的
      

  3.   

    我没用过ajax的 这个是综合网上的方法来凑合写的  请指出具体该如何写呢 谢谢
      

  4.   

    你写个回调函数吧
    function Add()
      {
      PageMethods.AddInfo(AddSeccce);
      }functin AddSeccce()
    {
      alert('添加成功');
    }
    这样试试
      

  5.   

    你叫一个Web Service到哪里去找HttpContext.Current.Request.QueryString啊?你还不如直接传递这些参数。
      

  6.   

    HttpContext.Current.Request 你可以在你的static方法中用,那才叫奇怪呢静态的方法中可以实例化吗?                不可以
    twoManager tm = new twoManager();
      

  7.   

    你的那些那个要传递的参数是可以放在
    PageMethods.AddInfo();
    这个当中的。还有你如果在静态方法中不能实例化,那你把那个要调用的类改为静态的类,方法也改为静态的方法。
      

  8.   

    public partial class WebForm1 : System.Web.UI.Page
      {
        protected void Page_Load(object sender, EventArgs e)
      {
         Ajax.Utility.RegisterTypeForAjax(typeof("WebForm1"));  }
      [System.Web.Services.WebMethod]
      public static void AddInfo()
      {
         ....
      }
      }
    <head id="Head1" runat="server">
      <title>无标题页</title>
      <script type="text/javascript">
      function Add()
      {
      WebForm1.AddInfo();
      }
      </script>
    </head>
    1.web.config中的配置
      <httpHandlers>
         <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/>
      </httpHandlers>
      

  9.   

    他那个是ASP.NET AJAx,不用像你这么搞的。楼主,你听10楼地~~~
      

  10.   

    都得变静态啊?那如果我这样呢
    public void AddInfo()
      {
      twoManager tm = new twoManager();
      two t = new two();
      t.Username = HttpContext.Current.Request.QueryString["username"].ToString();
      t.Userpwd = HttpContext.Current.Request.QueryString["userpwd"].ToString();
      t.Age = HttpContext.Current.Request.QueryString["age"].ToString();
      t.Sex = HttpContext.Current.Request.QueryString["sex"].ToString();
      tm.AddInfo(t);
      }
    --------------------------
    前台
     <input type="button" name="Button" value="添加" onclick="<%=AddInfo()%>"/>
      

  11.   

    有这么麻烦吗?
     <div>
      用户名<input type="text" name="username" id="uname" /><br />
      密码<input type="text" name="userpwd" id="upwd"/><br />
      年龄<input type="text" name="age" id="uage" /><br />
      性别<input type="text" name="sex" id="usex"/><br />
    <input type="button" name="Button" value="添加" onclick="Add($get('uname').value, $get('upwd').value, $get('uage').value, $get('usex').value"/>这么传不行吗?服务器端用string参数接着不就行了吗?$get('id').value是document.getElementById('id')的asp.net ajax缩写方式。
      

  12.   


     <input type="button" name="Button" value="添加" onclick="<%=AddInfo()%>"/>
      

  13.   

    你至于在这种地方跟我较劲吗?100个参数的时候你玩JSON啊,可问题是你这问题用得上JSON吗?你连$get('id').value都没学会,你就想到100个参数了? 一个讲asp.net ajax的书到了几乎接近最后章节的时候讲的内容...,您可真厉害,跟我太像了:-)