DataList控件使用页面方法删除命令事件执行 [英] Datalist Delete Command Event implementation using Page Methods

查看:126
本文介绍了DataList控件使用页面方法删除命令事件执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a DataList and Update Panel in my page. After implementation, I checked that the response is talking very long time after using Update panels...Here is the study material. I have a Delete Command event in Datalist and works find in the above mentioned case. I was trying to implement Delete Command using Page Methods. Any Idea how to do that?

我基本上想找到在此事件中隐藏控件,并有删除的记录在数据库`。任何帮助将是非常美联社preciated。

I basically want to find hidden controls in this event and have to delete the record in `database. Any help will be highly appreciated.

推荐答案

完整的应用程序可以从以下网址下载:

Rest Services

The full application can be downloaded from:

http://sdrv.ms/LJJz1K

本示例使用在ASP.Net(相同的概念可以应用到一个MVC应用程序)

This sample uses rest services in ASP.Net (the same concepts can be applied to a MVC application)

使用REST服务VS页面方法时,更清晰的优点是可测性。

The clearer advantage when using rest services vs page methods, is testability.

我会引导你一步一步地配置服务:

I will guide you step by step to configure the service:

您需要以下参考资料:


  • System.Web.ServiceModel.dll

  • System.Web.ServiceModel.Activati​​on.dll

  • System.Web.ServiceModel.Web.dll

的NuGet包:

  • jQuery

jQuery插件:

  • jQuery Block UI (it’s available as a single script file)

服务信息

[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中

In 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中

In web.config

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true"
          automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

注册脚本(它们可以在母版页中注册)

Register scripts (they can be registered in a master page)

<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内容页(在此示例中,我使用的是母版页)

In a ASP.Net content page (in this sample, I am using a master page)

<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命令最简单的方式=)

And that’s it, ajax commands the easy way =)

这篇关于DataList控件使用页面方法删除命令事件执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆