2

我的页面中有一个 DataList 和更新面板。实施后,我在使用更新面板后检查了响应是否在说话很长时间......这是学习材料。我在 Datalist 中有一个 Delete Command 事件,并且在上述情况下可以找到。我试图使用页面方法实现删除命令。任何想法如何做到这一点?

我基本上想在这个事件中找到隐藏的控件并且必须删除`database. 任何帮助将不胜感激。

4

1 回答 1

2

休息服务

完整的应用程序可以从以下位置下载:

http://sdrv.ms/LJJz1K

此示例使用 ASP.Net 中的 REST 服务(相同的概念可以应用于 MVC 应用程序)

使用休息服务与页面方法相比,更明显的优势是可测试性。

我将逐步指导您配置服务:

您需要以下参考资料:

  • System.Web.ServiceModel.dll
  • System.Web.ServiceModel.Activation.dll
  • System.Web.ServiceModel.Web.dll

Nuget 包:

jQuery插件:

服务信息

[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 命令很简单 =)

于 2012-06-10T08:28:52.417 回答