Asp.net 在Webmethod中获取Request.Form值

Asp.net 在Webmethod中获取Request.Form值,asp.net,Asp.net,我使用webmethod,而且 我使用Request.form,使用输入文本框中的want值 如何使用Ajax post在WebMethod中获取表单输入值 我的问题是,我将一些输入作为参数传递给webmethod,但希望通过使用Request.Form检索其他输入,但变量中的输入为空 我使用runat=“server”作为输入type=“text” 我试过了 string setrname = HttpContext.Current.Request.Form["selectroomname"]

我使用webmethod,而且

我使用Request.form,使用输入文本框中的want值

如何使用Ajax post在WebMethod中获取表单输入值

我的问题是,我将一些输入作为参数传递给webmethod,但希望通过使用Request.Form检索其他输入,但变量中的输入为空

我使用runat=“server”作为输入type=“text”

我试过了

string setrname = HttpContext.Current.Request.Form["selectroomname"];
但这是无效的

请指教
maulik

我假设您的
WebMethod
如下所示

[WebMethod]
public static string MyWebMethod(..
您无法访问静态方法中的
页面
控件,即
文本框
和其他控件,因为它们是非静态的

相反,您应该在ajax中从客户端本身传递文本框的值,即

   var url='/Default.aspx/MyWebMethod';
   var value = document.getElementById('textBoxId').value;
   var data={myField:value};
   makeAjaxRequest(url,data,callback);
   function callback(data){
      ///this is where you recieve ajax success
   }
其中,
makeAjaxRequest
是一个简单的ajax发送函数,类似于

 <script type="text/javascript">
        function makeAjaxRequest(url,data,callback) {
            $.ajax({
                type: 'POST',
                url: url,
                data: data,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: callback
            });
        }
    </script>

函数makeAjaxRequest(url、数据、回调){
$.ajax({
键入:“POST”,
url:url,
数据:数据,
contentType:'application/json;charset=utf-8',
数据类型:“json”,
成功:回调
});
}

处理以下代码,并让我知道您得到了什么输出:

int v = HttpContext.Current.Request.Form.Keys.Count;
        string var = "";
        string[] name1 = HttpContext.Current.Request.Form.AllKeys;
        if(name1 != null)
        {
        foreach (string name in name1)
        {
            if (name != null)
            {
                var = var + "[" + name.Trim() + "]-" + HttpContext.Current.Request.Form[name].ToString().Trim();
                var = var + "---";
               //store it and compare according to your value--selectroomname 
            }
        }

ASP.NET
中,
WebMethod
是静态函数,在静态方法中不能使用非静态字段,你不会得到任何东西,你最好的机会是通过ajax请求传递文本框值,但我添加了新的html(从jquery追加),并且可以有值数组。。我使用web服务,以便显示加载/搜索图像,并在后台填充数据表以获取所有request.form值,然后重定向。我使用ajax.post并返回url,然后使用window.location.hrefcalled@user2838660告诉我,您是否使用单独的
WebService
文件,即
.asmx
文件?否……在ajax帖子中:我使用hotels.aspx/BindValues,其中BindValues WebMethod位于hotels.aspx.csgreat中,请记住,
请求。表单[“key”]
不会给您任何东西,您应该尝试通过ajax本身传递文本框值,查看我的更新答案OK。然后我将传递参数。如果我想获得所有同名输入的值,比如,,…我想在jquery中使用name属性获得上述三个值,如何实现这一点:string[]name1=HttpContext.Current.Request.Form.AllKeys;我使用html中的textarea获取为空。。。并从jquery中添加新的textarea…因此对于exp。。我有一个textarea,我添加了新的textarea。。。第二个textarea的值保存在数据库中,第一个textarea的值为空。。。源代码中的第一个文本区域类似于。。。