使用 WebForms 进行 jQuery 验证网络服务
现在我有一个简单的表单,只有一个输入。我正在尝试使用 jQuery Validate 的远程部分来调用我的 web 服务,该服务现在只是返回 false。我现在遇到的问题是,当它调用我的网络服务时,它会提取输入的名称,这是 .net 创建的一些垃圾。有没有办法覆盖它并使用输入的 ID 和输入的名称。这是我的 jQuery:
$(function () {
$('form').validate();
$("#tbSiteName").rules("add", {
required: true,
remote: "webservices/webservice.asmx/HelloWorld"
});
});
这是我的 HTML:
<label for="tbSiteName">Name:</label>
<input name="ctl00$MainContent$tbSiteName" type="text" id="tbSiteName" class="required" />
这是来自 Chrome 的标头信息:(注意查询字符串参数)
Accept:application/json, text/javascript, */*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded
Cookie:ASP.NET_SessionId=qvupxcrg0yukekkni323dapj
Host:localhost:56803
Referer:http://localhost:56803/Default.aspx
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10
X-Requested-With:XMLHttpRequest
Query String Parameters
ctl00%24MainContent%24tbSiteName:ts
从服务器端,我收到 500 内部服务器错误,因为我的签名与我的帖子不匹配。
网络服务代码:
[WebMethod]
[ScriptMethod]
public bool HelloWorld(string tbSiteName) {
return tbSiteName.Length > 5;
}
感谢您的帮助。
Right now I have simple form with a single input. I'm trying to use the remote part of jQuery Validate to call my webservice that right now is just returning false. The problem I'm having right now is when it calls my webservice it is pulling the name of the input which is some garbage created by .net. Is there a way to override this and use the Id of the input verse the name of the input. Here is my jQuery:
$(function () {
$('form').validate();
$("#tbSiteName").rules("add", {
required: true,
remote: "webservices/webservice.asmx/HelloWorld"
});
});
Here is my HTML:
<label for="tbSiteName">Name:</label>
<input name="ctl00$MainContent$tbSiteName" type="text" id="tbSiteName" class="required" />
Here is the header info from Chrome: (notice the Query string params)
Accept:application/json, text/javascript, */*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded
Cookie:ASP.NET_SessionId=qvupxcrg0yukekkni323dapj
Host:localhost:56803
Referer:http://localhost:56803/Default.aspx
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10
X-Requested-With:XMLHttpRequest
Query String Parameters
ctl00%24MainContent%24tbSiteName:ts
From the server side I'm getting a 500 Internal server error because my signature doesn't match my post.
Webservice Code:
[WebMethod]
[ScriptMethod]
public bool HelloWorld(string tbSiteName) {
return tbSiteName.Length > 5;
}
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,据我所知,在使用 ASP.Net WebForms 服务器控件(例如
)时,无法解决此问题。尽管在 .NET 4 中您具有ClientIdMode="Static"
属性 (参见此处)禁用自动生成的客户端 ID,不会影响name
属性。Rick Strahl 在回应他的博客文章上的评论时建议,如果您确实需要可预测的名称,您应该只使用 html
控件:
您是否考虑过仅使用客户端
而不是
?此外,您是否考虑过放弃 ASP.NET WebForms 并使用 MVC? ;-)
Unfortunately, to my knowledge, there's no way to get around this when using ASP.Net WebForms server controls such as
<asp:textbox>
. Although in .NET 4 you have theClientIdMode="Static"
attribute (see here) to disable the auto-generated client IDs, that does not affect thename
attribute.Rick Strahl has suggested in response to comments on his blog post that if you really need predictable names, you should just use an html
<input>
control:Have you considered just using a client-side
<input>
instead of an<asp:textbox runat="server">
?Additionally, have you considered dropping ASP.NET WebForms and using MVC? ;-)
这就是我想出的答案。我必须将我的 asp:textbox 更改为 HTML 输入才能使其正常工作。另外,我必须将 web.config 更改为所有 HttpGet 到我的 web 服务。这充其量是痛苦的。我还通过使用标准输入丢失了控件的视图状态。我很想将其切换到 MVC,但这不是一个选择。
感谢您的帮助。
This is the answer I've came up with. I had to change my asp:textbox to a HTML input in order to get this to work. Also, I had to change the web.config to all HttpGet to my webservice. This is painful at best. I've also lost viewstate on the control by using a standard input. I'd love to switch this over to MVC, but it is just not an option.
Thanks for the help.