如何正确地与确认框radajaxmanager配置删除功能? [英] How to properly configure delete functionality with radajaxmanager with confirmation box?

查看:197
本文介绍了如何正确地与确认框radajaxmanager配置删除功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用中我与更新和删除功能以及显示的记录列表Telerik的Grid控件。

I am using Telerik Grid control in which i am displaying list of records along with Update and Delete functionality.

现在我想删除记录时,使用户犯规意外删除的记录,显示确认对话框。

Now i want to show confirmation box when deleting records so that user doesnt accidentally delete the record.

因此​​,这里是我的方法:

So here is my approach:

1)我有一个母版页即是的 MyMaster.Master 其中包含确认框一个普通的客户端的事件,我将使用sweetalert为:

1)I have one master page i.e is MyMaster.Master which contains one common client side events for confirmation box which i would be using sweetalert for that:

function DeleteData(Id) {
   var ajaxManager = null;
   var action = 'Remove';
   ajaxManager = $find("ctl00_cphMain_RadAjaxManager2");
   var arg = action + "," + Id; //Remove,1(1 indicates id of record to remove from grid)
   ajaxManager.ajaxRequest(arg);This line will fire below method.
  }

2)我创建1 Parentpage从我所有的网页会被继承和这个网页上我的 RadAjaxManager2_AjaxRequest 将驻留让我不要有污染我的每一页用这种方法,这方法将负责处理我的每个页面上删除功能:

2)I have created 1 Parentpage from which all of my page would be inherited and on this page my RadAjaxManager2_AjaxRequest would reside so that i dont have to pollute my every page with this method and this method will be responsible to handle my delete functionality on every page:

在此方法,我将通过我的删除方法fire.For例如名称: Remove1

In this Method i will pass name of my delete method to fire.For Eg:Remove1

public class ParentPage : RadAjaxPage
{
    protected void RadAjaxManager2_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
     var stringArray = e.Argument.Split(",".ToCharArray());//[0]="Remove1",[1]=id of record to delete
     RemoveRecord( stringArray[0], stringArray[1]);
     }
}

3)我的页面是Abc.aspx在那里我会把我删除功能,从数据库中删除bindgridview记录:

3)My Page that is Abc.aspx where i would place my delete functionality to remove record from database and bindgridview:

<telerik:RadAjaxManager ID="RadAjaxManager2" runat="server" OnAjaxRequest="RadAjaxManager2_AjaxRequest">
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="RadAjaxManager2">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="Grid1" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                </AjaxSettings>
            </telerik:RadAjaxManager>
    <ItemTemplate>
      <asp:ImageButton runat="server" ID="Remove1" Text="Delete" OnClientClick='<%# Eval("Id", "javascript:return DeleteData(\"{0}\");") %>' />
      </ItemTemplate>
         public partial class Abc : ParentPage
            {
               protected void Page_Load(object sender, EventArgs e)
                {
                }
                private void RemoveRecord( buttonId, recordId) 
                {
                   if(buttonId== "Remove1")
                   {
                      //code to delete record with id of recordId
                      //Bind grid view again to display latest records after performing deletion.
                   }   
                }
            }

这所有的工作完美的,但问题是,因为我们都知道,当我们从父页继承子页则称从子页面的方法,但在这里我的情况下,父页面方法这是我打电话对面
我的 RemoveRecord 从父页面的方法,这就是我认为这是不合适的。

All this is working perfect but the problem is as we all know that when we inherit child page from parent page then we call parent page method from child page method but here in my case this is opposite that is i am calling my RemoveRecord method from parent page and that is i think it is inappropriate.

所以我想克服this.How正确配置我删除功能???

So i want to overcome this.How to properly configure my delete functionality???

推荐答案

与code的问题是,你是混合了code的父类,子类和母版页。如果code实际上属于子类你把它的父类或母版页,这是一个不好的编程习惯肯定会造成很多的问题和错误。此外,您的code将是非常困难,也不可能维持。

The problem with your code is that you are mixing up the code for parent class, child class and master page. If code actually belongs to child class you are putting it in parent class or master page, which is a bad programming practice and will definitely cause a lot of issues and bugs. Also, your code would be very difficult and impossible to maintain.

既然你的 RadAjaxManager 的在你的子页面即 ABC.aspx ,因此事件的 RadAjaxManager2_AjaxRequest 的应该放在在子页面,而不是在父页面。你已经放置在父类,即此事件在 ParentPage 类。 这样做的原因进行了详细下面段落解释。此外,JavaScript方法的 DeleteData(ID)的不应该在母版页,但在实际的子页面,因为这种方法被称为响应点击删除子页上的按钮。

Since you have RadAjaxManager in your child page i.e. ABC.aspx, so the event RadAjaxManager2_AjaxRequest should be placed in child page and not in parent page. You have placed this event in parent class i.e. in ParentPage class. The reason for this is explained in detail in paragraph below. Also, the JavaScript method DeleteData(Id) should not be in master page but in the actual child page, since this method is called in response to clicking delete button on child page.

一个你必须在面向对象的程序设计遵循的规则是,一个类只能做它应该做的和不应该做的事情其他类负责。在你的情况, ParentPage 类应该只包含code,对于子类,而不是code,它实际上是一个子类的功能提供了基本功能。具体来说,删除的功能是子类的责任,而不是父类,因此所有code为删除功能应该是子类,也 RadAjaxManager 是子类的一部分,因此与 RadAjaxManager 包括事件的 RadAjaxManager2_AjaxRequest 的应在子类,而不是在父类。

One of the rules you must follow in object-oriented programming is that a class should only do what it's supposed to do and should not be doing what other classes are responsible for. In your case, ParentPage class should only contain code that provides base functionality for the child classes and not code that actually is functionality of a child class. Specifically speaking, the delete functionality is responsibility of child class and not parent class, so all code for delete functionality should be in child class, and also RadAjaxManager is part of child class, so all code related to RadAjaxManager including the event RadAjaxManager2_AjaxRequest should be in child class and not in parent class.

这样做可以让你的孩子写网页的删除功能,并没有违反面向对象编程的原则。这将很容易让你和其他程序员来维护您的code,同时你将尽量减少错误,因为你的code会干净,易于遵循。

Doing this will allow you to write the delete functionality in the child page and not violate the principles of object-oriented programming. This will make it easy for you and other programmers to maintain your code, and also you will minimize bugs since your code will be clean and easy to follow.

这篇关于如何正确地与确认框radajaxmanager配置删除功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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