📄 reorderlist.cs
字号:
}
// stuff the row into the newValues, we'll use it later.
//
values[KeysKey] = keys;
valuesList.Add(values);
}
finally
{
i++;
}
}
// now that we've got the values, swap them in the list.
// First, make the indexes zero-based.
//
oldIndex -= start;
newIndex -= start;
int startOrder = int.MinValue;
// figure out the current sort value of the highest item in the
// list.
//
if (valuesList.Count > 0 && valuesList[0].Contains(sortField))
{
object startValue = valuesList[0][sortField];
string startValueAsString;
if (startValue is int) {
// optimize the common case
//
startOrder = (int)startValue;
}
else if (null != (startValueAsString = startValue as string))
{
if (!Int32.TryParse(startValueAsString, NumberStyles.Integer, CultureInfo.InvariantCulture, out startOrder))
{
return false;
}
}
else
{
// handle all the various int flavors...
//
if (startValue != null && startValue.GetType().IsValueType && startValue.GetType().IsPrimitive)
{
startOrder = Convert.ToInt32(startValue, CultureInfo.InvariantCulture);
return true;
}
return false;
}
}
else
{
throw new InvalidOperationException("Couldn't find sort field '" + SortOrderField + "' in bound data.");
}
// start at zero if we couldn't find anything.
if (startOrder == int.MinValue)
{
startOrder = 0;
}
// swap the items in the list itself.
//
IOrderedDictionary targetItem = valuesList[oldIndex];
valuesList.RemoveAt(oldIndex);
valuesList.Insert(newIndex, targetItem);
// walk through each of them and update the source column
//
foreach (IOrderedDictionary values in valuesList)
{
// pull the keys back out.
//
IDictionary keys = (IDictionary)values[KeysKey];
// remove it from our values collection so it doesn't
// get based to the data source
//
values.Remove(KeysKey);
// Copy the current values to use as the old values.
//
IDictionary oldValues = CopyDictionary(values, null);
// update the sort index
//
values[sortField] = startOrder++;
// now call update with the new sort value.
//
dsv.Update(keys, values, oldValues,
delegate(int rowsAffected, Exception ex)
{
if (ex != null)
{
throw new Exception("Failed to reorder.", ex);
}
return true;
}
);
}
return true;
}
protected override void OnPreRender(EventArgs e)
{
// on pre render, see if an async call back happened.
// if so, flip requires data binding.
//
if (DataBindPending)
{
RequiresDataBinding = true;
}
base.OnPreRender(e);
}
/// <summary>
/// Get the template to give us the current values for each field we need.
/// </summary>
private void ExtractRowValues(IOrderedDictionary fieldValues, ReorderListItem item, bool includePrimaryKey, bool isAddOperation)
{
if (fieldValues == null) return;
// Which template are we using?
//
IBindableTemplate bindableTemplate = ItemTemplate as IBindableTemplate;
if (!isAddOperation)
{
switch (item.ItemType)
{
case ListItemType.Item:
break;
case ListItemType.EditItem:
bindableTemplate = EditItemTemplate as IBindableTemplate;
break;
default:
return;
}
}
else
{
bindableTemplate = InsertItemTemplate as IBindableTemplate;
}
if (bindableTemplate != null)
{
string keyName = DataKeyField;
// get the values from the template
//
IOrderedDictionary newValues = bindableTemplate.ExtractValues(item);
foreach (DictionaryEntry entry in newValues)
{
// put the value in unless it's the primary key, we get that elsewhere.
//
if (includePrimaryKey || 0 != String.Compare((string)entry.Key, keyName, StringComparison.OrdinalIgnoreCase))
{
fieldValues[entry.Key] = entry.Value;
}
}
}
}
/// <summary>
/// Creates our DropTemplate control. The DragDropList behavior uses a second UL control to
/// do the actual drags. That control has children that represent the item to use as the dropTemplate
/// or empty template. This method creates that structure.
/// </summary>
/// <param name="dropItem">Returns a reference to the item to use as the drop template</param>
/// <param name="emptyItem">Returns a reference to the item to use as the empty template.</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", Justification="Already returning a value")]
protected WebControl GetDropTemplateControl(out Control dropItem, out Control emptyItem)
{
dropItem = null;
emptyItem = null;
if (!AllowReorder || DesignMode) return null;
if (_dropTemplateControl == null)
{
BulletedList bl = new BulletedList();
// make sure it doesn't show up.
bl.Style["visibility"] = "hidden";
bl.Style["display"] = "none";
BulletedListItem dropAreaItem = new BulletedListItem();
dropAreaItem.ID = "_dat";
dropAreaItem.Style["vertical-align"] = "middle";
if (ReorderTemplate == null)
{
dropAreaItem.Style["border"] = "1px solid black";
}
else
{
ReorderTemplate.InstantiateIn(dropAreaItem);
}
dropItem = dropAreaItem;
bl.Controls.Add(dropAreaItem);
_dropTemplateControl = bl;
this.Controls.Add(bl);
}
else
{
dropItem = _dropTemplateControl.FindControl("_dat");
emptyItem = null;
}
return (WebControl)_dropTemplateControl;
}
/// <summary>
/// Walks the database to find the correct value for a new item inserted into the list.
/// </summary>
/// <returns>The sort value for the new item.</returns>
private int GetNewItemSortValue(out bool success)
{
DataSourceView dsv = GetData();
EventWaitHandle w = new System.Threading.EventWaitHandle(false, EventResetMode.AutoReset);
int newIndex = 0;
bool bSuccess = false;
dsv.Select(new DataSourceSelectArguments(),
delegate(IEnumerable dataSource)
{
try
{
// look for the first or last row, based on our InsertItemLocation
//
IList list = dataSource as IList;
if (list == null)
{
return;
}
if (0 == list.Count)
{
bSuccess = true;
return;
}
object row = null;
int delta = 1;
if (ItemInsertLocation == ReorderListInsertLocation.End)
{
row = list[list.Count - 1];
}
else
{
row = list[0];
delta = -1;
}
// get the sort index value of that row
//
PropertyDescriptor rowProp = TypeDescriptor.GetProperties(row)[SortOrderField];
if (rowProp != null)
{
object rowValue = rowProp.GetValue(row);
if (rowValue is int)
{
newIndex = (int)rowValue + delta;
bSuccess = true;
}
}
}
finally
{
w.Set();
}
}
);
// wait for the select to finish.
//
w.WaitOne();
success = bSuccess;
return newIndex;
}
// Called when a "Cancel" command is fired.
//
private void HandleCancel(ReorderListCommandEventArgs e)
{
if (IsBoundUsingDataSourceID)
{
EditItemIndex = -1;
RequiresDataBinding = true;
}
OnCancelCommand(e);
}
// Called when a "Delete" command is fired.
//
private void HandleDelete(ReorderListCommandEventArgs e)
{
if (IsBoundUsingDataSourceID)
{
DataSourceView view = GetData();
if (view != null)
{
IDictionary oldValues;
IOrderedDictionary newValues;
IDictionary keys;
// pick up the current values
//
PrepareRowValues(e, out oldValues, out newValues, out keys);
// do the delete
//
view.Delete(keys, oldValues,
delegate(int rows, Exception ex)
{
if (ex != null) return false;
OnDeleteCommand(e);
return true;
}
);
return;
}
}
OnDeleteCommand(e);
RequiresDataBinding = true;
}
// Called when a "Edit" command is fired.
//
private void HandleEdit(ReorderListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
EditItemIndex = e.Item.ItemIndex;
RequiresDataBinding = true;
}
OnEditCommand(e);
}
private void HandleInsert(ReorderListCommandEventArgs e)
{
if (IsBoundUsingDataSourceID && SortOrderField != null)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -