rad grid, drag and drop
来源:互联网 发布:允儿好看还是js卡好看 编辑:程序博客网 时间:2024/06/04 08:20
<%@ Page Title="" Language="C#" MasterPageFile="~/Masters/admin.master" AutoEventWireup="true" CodeFile="testDragDrop.aspx.cs" Inherits="Admin_policy_testDragDrop" %><%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><%--<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />--%> <telerik:RadFormDecorator ID="RadFormDecorator1" DecoratedControls="All" runat="server" Skin="Metro" EnableRoundedCorners="false"></telerik:RadFormDecorator> <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Width="256px" Height="64px" MinDisplayTime="5000"> <asp:Label ID="Label2" runat="server" ForeColor="Red">Loading... </asp:Label> </telerik:RadAjaxLoadingPanel><%-- <asp:Image ID="Image1" runat="server" Width="224px" Height="48px" ImageUrl="~/Loading.gif"></asp:Image>--%> <telerik:RadAjaxManager runat="server" ID="radAjax" DefaultLoadingPanelID="RadAjaxLoadingPanel1"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="grdPolicy"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="grdPolicy" ></telerik:AjaxUpdatedControl> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings> </telerik:RadAjaxManager> <telerik:RadScriptBlock runat="server" ID="scriptBlock"> <script type="text/javascript"> //<![CDATA[ function onRowDropping(sender, args) { if (sender.get_id() == "<%=grdPolicy.ClientID %>") { var node = args.get_destinationHtmlElement(); if (!isChildOf('<%=grdPolicy.ClientID %>', node)) { args.set_cancel(true); } } } function isChildOf(parentId, element) { while (element) { if (element.id && element.id.indexOf(parentId) > -1) { return true; } element = element.parentNode; } return false; } //]]> </script> </telerik:RadScriptBlock> <div class="exWrap"> <p class="howto"> Select and drag orders from pending to shipped when dispatched<br /> Reorder pending orders on priority by drag and drop<br /> Drop a shipped order over the recycle bin to delete it</p> <div style="float: left; padding: 0 6px 0 10px"> <h2 style="color: #9c3608"> Pending Orders</h2> <telerik:RadGrid runat="server" ID="grdPolicy" AutoGenerateColumns="false" AllowPaging="True" Width="350px" AllowMultiRowSelection="true" PageSize="30" onneeddatasource="grdPolicy_NeedDataSource" onrowdrop="grdPolicy_RowDrop" ondeletecommand="grdPolicy_DeleteCommand" onitemcommand="grdPolicy_ItemCommand" > <MasterTableView DataKeyNames="PolicyId" Width="100%" TableLayout="Fixed" CssClass="table table-bordered table-primary" > <Columns> <telerik:GridDragDropColumn HeaderStyle-Width="18px" Visible="false"> </telerik:GridDragDropColumn> <telerik:GridBoundColumn HeaderText="Policy Name" DataField="Name"></telerik:GridBoundColumn> <telerik:GridTemplateColumn> <ItemTemplate> <asp:HyperLink runat="server" NavigateUrl='<%# Eval("PolicyId","edit.aspx?PolicyId={0}") %>' CssClass="btn-action no-border glyphicons edit btn-table"><i></i></asp:HyperLink> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn> <ItemTemplate> <asp:LinkButton ID="BtnDelete" runat="server" CssClass="btn-action no-border glyphicons remove_2 btn-table" CommandArgument='<%# Eval("PolicyId") %>' CommandName="Delete" OnClientClick="javascript: return confirm('Are you sure?');"><i></i></asp:LinkButton> </ItemTemplate> </telerik:GridTemplateColumn> </Columns> </MasterTableView> <ClientSettings AllowRowsDragDrop="True" AllowColumnsReorder="true" ReorderColumnsOnClient="true"> <Selecting AllowRowSelect="True" EnableDragToSelectRows="false"></Selecting> <ClientEvents OnRowDropping="onRowDropping"></ClientEvents> <Scrolling AllowScroll="true" UseStaticHeaders="true"></Scrolling> </ClientSettings> <%--<PagerStyle Mode="NumericPages" PageButtonCount="4"></PagerStyle>--%> </telerik:RadGrid> </div> <div style="clear: both;"> </div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </div></asp:Content>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Web.UI;using Telerik.Web.UI;using HRASP.Entities;using HR.Managers.Library.Managers;public partial class Admin_policy_testDragDrop : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { grdPolicy.MasterTableView.GetColumn("DragDropColumn").Visible = true; } protected TList<Policy> Policies { get { try { object obj = Session["Policies"]; if (obj == null) { obj = PolicyManager.GetAllPolicies(); if (obj != null) { Session["Policies"] = obj; } else { obj = new TList<Policy>(); } } return (TList<Policy>)obj; } catch (Exception ex) { Session["PendingPolicys"] = null; } return new TList<Policy>(); } set { Session["Policies"] = value; } } protected void grdPolicy_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) { grdPolicy.DataSource = Policies; } private void UpdateSortOrder() { TList<Policy> list = (TList<Policy>)Policies; PolicyManager.UpdatePoliciesSortOrder(list); } protected void grdPolicy_RowDrop(object sender, GridDragDropEventArgs e) { if (string.IsNullOrEmpty(e.HtmlElement)) { // check drag from which grid if (e.DraggedItems[0].OwnerGridID == grdPolicy.ClientID) { // check drop to whcih grid if (e.DestDataItem != null && e.DestDataItem.OwnerGridID == grdPolicy.ClientID) { //rePolicy items in pending grid TList<Policy> list = Policies; var obj1 = e.DestDataItem.GetDataKeyValue("PolicyId"); Policy p = GetPolicy(list, (Guid)e.DestDataItem.GetDataKeyValue("PolicyId")); int destinationIndex = list.IndexOf(p); if (e.DropPosition == GridItemDropPosition.Above && e.DestDataItem.ItemIndex > e.DraggedItems[0].ItemIndex) { destinationIndex -= 1; } if (e.DropPosition == GridItemDropPosition.Below && e.DestDataItem.ItemIndex < e.DraggedItems[0].ItemIndex) { destinationIndex += 1; } TList<Policy> PolicysToMove = new TList<Policy>(); foreach (GridDataItem draggedItem in e.DraggedItems) { Policy tmpPolicy = GetPolicy(list, (Guid)draggedItem.GetDataKeyValue("PolicyId")); if (tmpPolicy != null) PolicysToMove.Add(tmpPolicy); } foreach (Policy PolicyToMove in PolicysToMove) { list.Remove(PolicyToMove); list.Insert(destinationIndex, PolicyToMove); } Policies = list; grdPolicy.Rebind(); //int destinationItemIndex = destinationIndex - (grdPendingPolicys.PageSize * grdPendingPolicys.CurrentPageIndex); //e.DestinationTableView.Items[destinationItemIndex].Selected = true; } } } } private static Policy GetPolicy(IEnumerable<Policy> PolicysToSearchIn, Guid policyId) { foreach (Policy Policy in PolicysToSearchIn) { if (Policy.PolicyId == policyId) { return Policy; } } return null; } protected void Button1_Click(object sender, EventArgs e) { UpdateSortOrder(); } protected void grdPolicy_DeleteCommand(object sender, GridCommandEventArgs e) { } protected void grdPolicy_ItemCommand(object sender, GridCommandEventArgs e) { //if (e.CommandName == "Delete") //{ // string s = e.CommandArgument.ToString(); //} try { Guid uid = new Guid(); if (Guid.TryParse(e.CommandArgument.ToString(), out uid)) { if (uid != Guid.Empty) { if (e.CommandName == "Delete") { PolicyManager.DeletePolicy(uid); Policies = null; // get latest data from db } } } } catch (Exception ex) { } }}