我的页面中有一个数据列表和更新面板。实现后,我检查了使用更新面板后响应的通话时间很长... Here is the study material 。我在数据列表中有一个删除命令事件,并且在上述情况下可以找到。我试图使用页面方法来实现删除命令。知道如何做到这一点吗?
我基本上想在这个事件中找到隐藏的控件,并且必须删除`database.txt'中的记录。任何帮助将不胜感激。
最佳答案
休息服务
完整的应用程序可以从以下位置下载:
此示例使用 ASP.Net 中的 REST 服务(相同的概念可以应用于 MVC 应用程序)
使用休息服务与页面方法相比,更明显的优势是可测试性。
我将逐步指导您配置服务:
您需要以下引用资料:
- System.Web.ServiceModel.dll
- System.Web.ServiceModel.Activation.dll
- System.Web.ServiceModel.Web.dll
Nuget 包:
jQuery 插件:
- jQuery Block UI (它作为单个脚本文件提供)
服务信息
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "/DeleteFromService",
Method = "DELETE")]
void Delete(int id);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public void Delete(int id)
{
// delete your product
// simulate a long process
Thread.Sleep(5000);
}
}
在 Global.asax 中
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");
RouteTable.Routes.Add(new ServiceRoute("",
new WebServiceHostFactory(),
typeof(MyService)));
}
在 web.config 中
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
注册脚本(可以在母版页中注册)
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js" language="javascript" ></script>
<script language="javascript" type="text/javascript" src="Scripts/jquery.blockui.1.33.js"></script>
在 ASP.Net 内容页面中(在本示例中,我使用母版页)
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<input type="button" value="Delete" id="myButton" />
</asp:Content>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" language="javascript">
function deleteFromService() {
if (!confirm("Are you sure you want to delete?")) {
return;
}
$.blockUI();
$.ajax({
cache: false,
type: "DELETE",
async: true,
url: "/DeleteFromService",
data: "3", // get your id to delete
contentType: "application/json",
dataType: "json",
success: function () {
$(document).ajaxStop($.unblockUI);
alert("done");
},
error: function (xhr) {
$(document).ajaxStop($.unblockUI);
alert(xhr.responseText);
}
});
}
jQuery().ready(function () {
$("#myButton").click(deleteFromService);
});
</script>
</asp:Content>
就是这样,ajax 命令很简单 =)
关于javascript - 使用页面方法实现 Datalist 删除命令事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893954/