I am trying to reorder items with drag/drop - but e.DestDataItem is null in my RadGrid1_RowDrop event and I can't figure out why.
The example works for me (e.DestDataItem is not null), but I can't tell what the difference is between my code and the example. If someone could help me out I would greatly appreciate it.
The example works for me (e.DestDataItem is not null), but I can't tell what the difference is between my code and the example. If someone could help me out I would greatly appreciate it.
| using System; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.Configuration; |
| using System.Data; |
| using Telerik.Web.UI; |
| using System.Web; |
| using System.Web.Security; |
| using System.Web.UI; |
| using System.Web.UI.HtmlControls; |
| using System.Web.UI.WebControls; |
| using System.Web.UI.WebControls.WebParts; |
| using NewMillennium; |
| using NewMillennium.DataAccess; |
| public partial class Admin_UserControls_OrderFields : MillenniumUserControl |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| RadGrid1.Skin = Common.GridSkin; |
| if (!Page.IsPostBack) |
| { |
| WebProgramFieldSessionData.ClearSessionData(); |
| RefreshGrid(); |
| } |
| } |
| private void RefreshGrid() |
| { |
| RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace; |
| RadGrid1.DataSource = WebProgramFieldSessionData.GetWebProgramFields(ProjectProgramID); |
| RadGrid1.Rebind(); |
| EditAllRows(); |
| } |
| protected void EditAllRows() |
| { |
| foreach (GridDataItem item in RadGrid1.Items) |
| item.Edit = true; |
| RadGrid1.Rebind(); |
| } |
| protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) |
| { |
| RadGrid1.DataSource = WebProgramFieldSessionData.GetWebProgramFields(ProjectProgramID); |
| } |
| protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) |
| { |
| if (e.CommandName == "Refresh") |
| { |
| SaveChangesInSession(); |
| } |
| } |
| public void SaveChangesInSession() |
| { |
| foreach (GridItem item in RadGrid1.Items) |
| { |
| if (item is GridDataItem && item.IsInEditMode) |
| { |
| GridDataItem gridDataItem = item as GridDataItem; |
| int index = gridDataItem.DataSetIndex; |
| WebProgramField field = new WebProgramField(WebProgramFieldSessionData.WebProgramFields.Rows[index]); |
| field.FieldLabel = (gridDataItem["FieldLabel"].Controls[1] as TextBox).Text; |
| field.SortOrder = Convert.ToInt32((gridDataItem["SortOrder"].Controls[1] as TextBox).Text); |
| field.Visible = (gridDataItem["Visible"].Controls[0] as CheckBox).Checked; |
| field.Required = (gridDataItem["Required"].Controls[0] as CheckBox).Checked; |
| WebProgramFieldSessionData.Update(gridDataItem.DataSetIndex, field); |
| } |
| } |
| WebProgramFieldSessionData.UpdateSortOrder(); |
| RefreshGrid(); |
| } |
| public void SaveChangesInDatabase() |
| { |
| WebProgramFieldSessionData.SaveChangesInDatabase(); |
| } |
| protected void RadGrid1_RowDrop(object sender, GridDragDropEventArgs e) |
| { |
| if (string.IsNullOrEmpty(e.HtmlElement)) |
| { |
| if (e.DestDataItem != null && e.DestDataItem.OwnerGridID == RadGrid1.ClientID) |
| { |
| //TODO: do stuff here |
| } |
| } |
| } |
| private int ProjectProgramID |
| { |
| get |
| { |
| try |
| { |
| return Convert.ToInt32(Request.QueryString["ProjectProgramID"]); |
| } |
| catch |
| { |
| throw new ArgumentException("WebProgramFields.ascx requires a valid ProjectProgramID in the querystring."); |
| } |
| } |
| } |
| } |
| <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebProgramFields.ascx.cs" |
| Inherits="Admin_UserControls_OrderFields" %> |
| <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> |
| <asp:Label runat="server" ID="lblTitle"></asp:Label> |
| <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowSorting="False" |
| AutoGenerateColumns="False" AllowMultiRowEdit="true" AllowMultiRowSelection="true" |
| OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand" |
| OnRowDrop="RadGrid1_RowDrop"> |
| <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> |
| <ClientSettings AllowRowsDragDrop="True"> |
| <Selecting AllowRowSelect="True" EnableDragToSelectRows="true" /> |
| </ClientSettings> |
| <MasterTableView CommandItemDisplay="Top" DataKeyNames="FieldID" AutoGenerateColumns="False"> |
| <CommandItemTemplate> |
| <div style="padding-bottom: 8px"> |
| <asp:LinkButton ID="RefreshButton" runat="server" CommandName="Refresh" OnClientClick="suppressConfirmation()"><img class="middle" alt="" src="../Images/Telerik/Refresh.gif" /> Refresh </asp:LinkButton> |
| </div> |
| </CommandItemTemplate> |
| <Columns> |
| <telerik:GridBoundColumn SortExpression="DefaultFieldLabel" HeaderText="Field Label" |
| HeaderButtonType="TextButton" DataField="DefaultFieldLabel" ReadOnly="true"> |
| <HeaderStyle HorizontalAlign="Left" /> |
| </telerik:GridBoundColumn> |
| <telerik:GridCheckBoxColumn SortExpression="FieldVisible" HeaderText="Visible" HeaderButtonType="TextButton" |
| DataField="FieldVisible" UniqueName="Visible" DataType="System.Boolean"> |
| <HeaderStyle Width="50px" HorizontalAlign="center" /> |
| <ItemStyle HorizontalAlign="center" /> |
| </telerik:GridCheckBoxColumn> |
| <telerik:GridCheckBoxColumn SortExpression="FieldRequired" HeaderText="Required" |
| HeaderButtonType="TextButton" DataField="FieldRequired" UniqueName="Required" |
| DataType="System.Boolean"> |
| <HeaderStyle Width="50px" HorizontalAlign="center" /> |
| <ItemStyle HorizontalAlign="center" /> |
| </telerik:GridCheckBoxColumn> |
| <telerik:GridTemplateColumn SortExpression="SortOrder" HeaderText="Sort Order" HeaderButtonType="TextButton" |
| DataField="SortOrder" UniqueName="SortOrder"> |
| <HeaderStyle HorizontalAlign="center" /> |
| <ItemStyle HorizontalAlign="center" Width="80px" /> |
| <ItemTemplate> |
| <asp:TextBox ID="SortOrder" runat="server" Width="80px" Text='<%# Bind("SortOrder") %>'></asp:TextBox> |
| </ItemTemplate> |
| </telerik:GridTemplateColumn> |
| <telerik:GridTemplateColumn SortExpression="FieldLabel" HeaderText="Alternate Field Label" |
| HeaderButtonType="TextButton" DataField="FieldLabel" UniqueName="FieldLabel"> |
| <HeaderStyle HorizontalAlign="center" /> |
| <ItemStyle HorizontalAlign="center" Width="80px" /> |
| <ItemTemplate> |
| <asp:TextBox ID="FieldLabel" runat="server" Width="200px" Text='<%# Bind("FieldLabel") %>'></asp:TextBox> |
| </ItemTemplate> |
| </telerik:GridTemplateColumn> |
| </Columns> |
| </MasterTableView> |
| </telerik:RadGrid> |
| public class WebProgramFieldSessionData |
| { |
| public static DataTable GetWebProgramFields(int projectProgramID) |
| { |
| ProjectProgramID = projectProgramID; |
| return WebProgramFields; |
| } |
| public static void ClearSessionData() |
| { |
| System.Web.HttpContext.Current.Session["WebProgramFields"] = null; |
| System.Web.HttpContext.Current.Session["WebProgramFields_ProjectProgramID"] = null; |
| } |
| public static DataTable WebProgramFields |
| { |
| get |
| { |
| try |
| { |
| if (System.Web.HttpContext.Current.Session["WebProgramFields"] != null) |
| { |
| return System.Web.HttpContext.Current.Session["WebProgramFields"] as DataTable; |
| } |
| else |
| { |
| DataTable t = WebProgramField.GetWebProgramFieldSettingsAsDataTable(ProjectProgramID); |
| System.Web.HttpContext.Current.Session["WebProgramFields"] = t; |
| return t; |
| } |
| } |
| catch (Exception ex) |
| { |
| throw new ApplicationException("The web program fields could not be initialized because of an error: " + ex.Message); |
| } |
| } |
| } |
| public static int ProjectProgramID |
| { |
| get |
| { |
| if (System.Web.HttpContext.Current.Session["WebProgramFields_ProjectProgramID"] == null) |
| System.Web.HttpContext.Current.Session["WebProgramFields_ProjectProgramID"] = 0; |
| return Convert.ToInt32(System.Web.HttpContext.Current.Session["WebProgramFields_ProjectProgramID"]); |
| } |
| set |
| { |
| System.Web.HttpContext.Current.Session["WebProgramFields_ProjectProgramID"] = value; |
| } |
| } |
| /// <summary> |
| /// Updates a row in session (not in the database). |
| /// </summary> |
| public static void Update(int index, WebProgramField field) |
| { |
| DataRow row = WebProgramFields.Rows[index]; |
| row["FieldLabel"] = field.FieldLabel; |
| row["SortOrder"] = field.SortOrder; |
| row["FieldVisible"] = field.Visible; |
| row["FieldRequired"] = field.Required; |
| } |
| /// <summary> |
| /// Corrects the sort order for all items in session. |
| /// This method does not save anything in the database. |
| /// </summary> |
| public static void UpdateSortOrder() |
| { |
| WebProgramFields.DefaultView.Sort = "SortOrder asc"; |
| int counter = 1; |
| foreach (DataRowView row in WebProgramFields.DefaultView) |
| { |
| row["SortOrder"] = counter; |
| counter++; |
| } |
| } |
| public static void SaveChangesInDatabase() |
| { |
| foreach (DataRow row in WebProgramFields.Rows) |
| { |
| WebProgramField field = new WebProgramField(row); |
| field.Save(); |
| } |
| } |
| } |