Web 方法适用于 html 按钮,但不适用于 asp:button
我正在查看 this< 中的代码/a> 关于 Web 方法的文章,我已经尝试实现它。 这篇文章按设计工作,但是当我将 HTML 按钮替换为 asp:button 时,我得到了不同的结果。
代码背后:
[WebMethod]
public static string GetCurrentDate()
{
return DateTime.Now.ToString();
}
Aspx page:
js:
<script type="text/javascript">
function UpdateTime() {
$get('Label6').innerHTML = PageMethods.GetCurrentDate(OnSucceeded,
OnFailed);
}
function OnSucceeded(result, userContext, methodName) {
$get('Label6').innerHTML = result;
}
function OnFailed(error, userContext, methodName) {
$get('Label6').innerHTML = "An error occured.";
}
</script>
和其他标记
<form id="form1" runat="server">
<asp:ScriptManager EnablePageMethods="true"
ID="ScriptManager2" runat="server">
</asp:ScriptManager>
<div id="usingWebMethods">
<asp:Label runat="server"
Text="Using WebMethods"
ID="Label5" />
<br />
<asp:Label
runat="server"
Text="Web Method Update!"
ID="Label6" />
<asp:Button ID="Button3"
runat="server"
Text="Postback Update"
OnClientClick="UpdateTime();"
UseSubmitBehavior="False" />
<input type="button"
id="Button4"
value="Web Method Update"
onclick="UpdateTime();" />
</div>
</form>
我注意到按钮有两件事。该按钮返回发生错误,即使标签更改,视图状态也不会更新。
为什么会出现这两个问题以及我该如何解决这些问题?
I'm am looking at the code in this article on Web methods and I've tried to implement it.
The article works as designed but when I swap the HTML button for an asp:button I get different results.
CODE behind:
[WebMethod]
public static string GetCurrentDate()
{
return DateTime.Now.ToString();
}
Aspx page:
js:
<script type="text/javascript">
function UpdateTime() {
$get('Label6').innerHTML = PageMethods.GetCurrentDate(OnSucceeded,
OnFailed);
}
function OnSucceeded(result, userContext, methodName) {
$get('Label6').innerHTML = result;
}
function OnFailed(error, userContext, methodName) {
$get('Label6').innerHTML = "An error occured.";
}
</script>
and other markup
<form id="form1" runat="server">
<asp:ScriptManager EnablePageMethods="true"
ID="ScriptManager2" runat="server">
</asp:ScriptManager>
<div id="usingWebMethods">
<asp:Label runat="server"
Text="Using WebMethods"
ID="Label5" />
<br />
<asp:Label
runat="server"
Text="Web Method Update!"
ID="Label6" />
<asp:Button ID="Button3"
runat="server"
Text="Postback Update"
OnClientClick="UpdateTime();"
UseSubmitBehavior="False" />
<input type="button"
id="Button4"
value="Web Method Update"
onclick="UpdateTime();" />
</div>
</form>
I've noticed 2 things with the button. The button returns an error occurred and even though the label changes the viewstate is not updated.
Why do these 2 issues occur and what can I do to resolve them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ViewState 仅在回发时更新。您正在使用真正的 AJAX。它无法知道你做了什么。
ViewState only updates on postback. You are using true AJAX. It has no way of knowing what you did.