![](/img/trans.png)
[英]ASP.NET: How do I set ListView data through the codebehind instead of using the Bind() function in the Text attribute?
[英]ASP.NET 4.0 - Using Jquery, how do I call a codebehind function and return data to the client?
当我单击以下div时:
<div id="Result">Click here for the time.</div>
我需要以下代码隐藏函数才能运行:
<WebMethod()> _
Public Shared Function GetDate() As String
Return DateTime.Now.ToString()
End Function
我需要用GetDate()函数返回的字符串填充div的内部。 我认为这应该使用类似于以下的代码:
<script type="text/javascript">
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
$.ajax({
type: "POST",
url: "test.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
</script>
我已经从以下站点提取了此示例: http : //encosia.com/2008/05/29/using-jquery-to-direct-call-aspnet-ajax-page-methods/
但是,我根本无法正常工作。 什么都没发生。 这只是一个常规的asp.net Web项目。 除了在我的标记中包含脚本标签以引用jquery之外,我没有做过任何形式的Ajax支持业务。
单击div时,firebug控制台会告诉我以下内容:
POST http://admin/Default.aspx GetDate 404 Not Found -18ms
编辑:注意:test.aspx / GetDate必须与您的aspx页面名称和函数名称匹配!
<WebMethod()> _
Public Shared Function GetDate() As String
Return DateTime.Now.ToString()
End Function
必须“共享”(在C#中为“静态”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.