Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

javascript - Datalist Delete Command Event implementation using Page Methods

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rest Services

The full application can be downloaded from:

http://sdrv.ms/LJJz1K

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

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

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

You need the following references:

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

Nuget packages:

jQuery plugins:

Service info

[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);
    }
}

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)));

}

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>

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...