I have a RadComboBox inside Radgrid.
i need load items ondemand, but OnItemrequest does not fire.
The grid is a usercontrol.
This is my code
-ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FileNameRule.ascx.cs"
Inherits="WebSearch.UI.Admin.UserControls.Library.FileNameRule" %>
<link href="/UI/Style/Admin/UserControls/FileNameRule.css" rel="stylesheet" type="text/css" />
<div class="exampleView">
<div class="title">
<asp:Label ID="lblShowAsTitle" runat="server" />
</div>
<div class="label">
<asp:Label ID="lblShowAS" runat="server" />
</div>
</div>
<div class="clear">
</div>
<div class="exampleView">
<div class="clear">
</div>
<telerik:RadGrid runat="server" ID="MyGrid" AutoGenerateColumns="false" Skin="Vista"
AllowMultiRowSelection="True" PageSize="10" AllowPaging="true" EnableViewState="false">
<MasterTableView DataKeyNames="LibraryId,Id" CommandItemDisplay="Top" EditMode="InPlace">
<Columns>
<telerik:GridClientSelectColumn CommandName="Select" UniqueName="Select" HeaderStyle-Width="30px"
Resizable="false" />
<telerik:GridEditCommandColumn HeaderStyle-Width="25px" UniqueName="EditCommandColumn"
ButtonType="ImageButton" EditImageUrl="/UI/Images/grid_edit.png" Resizable="false">
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn UniqueName="btnDelete" ConfirmDialogType="RadWindow" ButtonType="ImageButton"
CommandName="Delete" ConfirmDialogHeight="100px" ConfirmDialogWidth="300px" HeaderStyle-Width="25px"
Resizable="false" ImageUrl="/UI/Images/cross.png" />
<telerik:GridTemplateColumn DataField="Id" HeaderText="Id" UniqueName="Id" Visible="false"
ConvertEmptyStringToNull="true">
<InsertItemTemplate>
<telerik:RadTextBox ID="lblId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %> '
Width="150px" ReadOnly="true" Enabled="false" CssClass="labelGrid" />
</InsertItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %> '
Width="150px" CssClass="labelGrid" ReadOnly="true" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="Position" HeaderText="Position" UniqueName="Position" Visible="false" HeaderStyle-Width="1px"
ConvertEmptyStringToNull="true">
<EditItemTemplate>
<asp:Label ID="lblPosition" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Position") %> '
Width="150px" CssClass="labelGrid" ReadOnly="true" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Position") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Type" DataField="TypeId">
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="cbType" EnableLoadOnDemand="True" EnableAutomaticLoadOnDemand="true" DataTextField="CDetailText"
DataValueField="CDetailId" HighlightTemplatedItems="true" Width="90%" CausesValidation="false" AutoPostBack="true" OnSelectedIndexChanged="cbType_SelectedIndexChanged"/>
<br />
<asp:RequiredFieldValidator ID="frSecurity" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="cbType"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDatatype" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Type") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Value" DataField="ValueId">
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="cbValue" EnableLoadOnDemand="True" DataTextField="CDetailText"
DataValueField="CDetailId" HighlightTemplatedItems="true" Width="90%" CausesValidation="false" />
<telerik:RadComboBox runat="server" ID="cbField" DataTextField="Header"
DataValueField="Id" HighlightTemplatedItems="true" Width="90%" CausesValidation="false" ItemsPerRequest="10"
ShowMoreResultsBox="true" EnableVirtualScrolling="true" EnableLoadOnDemand="True" />
<telerik:RadTextBox runat="server" ID="txtValueInput"></telerik:RadTextBox>
<br />
<asp:CustomValidator ID="cvValue" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="cbValue" OnServerValidate="cvValue_ServerValidate"></asp:CustomValidator>
<asp:CustomValidator ID="cvField" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="cbField" OnServerValidate="cvField_ServerValidate"></asp:CustomValidator>
<asp:CustomValidator ID="cvValueInput" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="txtValueInput" OnServerValidate="cvValueInput_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblValue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ValueName") %>'></asp:Label>
<asp:Label ID="lblFieldName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FieldName") %>'></asp:Label>
<asp:Label ID="lbValueInput" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Value") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<CommandItemTemplate>
<div class="quickAdd">
<asp:LinkButton ID="lnkqQuitInsert" runat="server" CommandName="InitInsert">
<asp:ImageButton ID="imgQuickAdd" runat="server" ImageUrl="/UI/Images/add.png" />
<asp:Label ID="lblQuickAdd" runat="server" /></asp:LinkButton>
<asp:LinkButton ID="lnkDeleteSelected" CommandName="SelectRow" runat="server" OnClick="DeleteSelectedItems">
<asp:ImageButton ID="imgDeleteSelected" runat="server" ImageUrl="/UI/Images/cross.png" />
<asp:Label ID="lblDeleteSelected" runat="server" /></asp:LinkButton>
<asp:LinkButton ID="lnkDeleteAllValues" runat="server" OnClick="DeleteValues">
<asp:ImageButton ID="imgDeleteAllValues" runat="server" ImageUrl="/UI/Images/delete_all.png" />
<asp:Label ID="lblDeleteAllValues" runat="server" /></asp:LinkButton>
</div>
</CommandItemTemplate>
</MasterTableView>
<ClientSettings AllowRowsDragDrop="True">
<Selecting AllowRowSelect="True" EnableDragToSelectRows="false" />
<ClientEvents OnGridCreated="GridCreated" />
</ClientSettings>
<PagerStyle Mode="NumericPages" />
</telerik:RadGrid>
</div>
.ascx.cs
public partial class FileNameRule : UserControl
{
#region Properties&Variables
private string _culture;
private ResourceManager _resources;
private RadAjaxManager AjaxManager { get; set; }
private RadAjaxLoadingPanel Loading { get; set; }
public List<WSFileNameRule> GetFileNameRuleList
{
get { return GetStringMaskList(); }
set { LoadData(value); }
}
#endregion
#region Events
protected void Page_Load(object sender, EventArgs e)
{
LoadAjax();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
GetInfo();
LoadSettings();
MyGrid.NeedDataSource += MyGrid_NeedSource;
MyGrid.InsertCommand += MyGrid_InsertCommand;
MyGrid.ItemCommand += MyGrid_ItemCommand;
MyGrid.DeleteCommand += MyGrid_DeleteCommand;
MyGrid.UpdateCommand += MyGrid_Updatecommand;
MyGrid.ItemCreated += MyGrid_ItemCreated;
MyGrid.ItemDataBound += MyGrid_ItemDataBound;
MyGrid.RowDrop += MyGrid_RowDrop;
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
switch (e.Argument)
{
case "RebindAndNavigate":
RebindGrid();
break;
case "DeleteAll":
Session["FileNameRule"] = null;
MyGrid.DataSource = new List<WSFileNameRule>();
MyGrid.Rebind();
break;
case "DeleteSelected":
DeleteItSelectedItems();
break;
}
}
#endregion
#region Methods
#region MyGridEvents
protected void MyGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode || (e.Item is IGridInsertItem))
{
LoadComboType(e);
LoadComboValue(e);
LoadComboField(e);
(e.Item.FindControl("cbValue")).Visible = false;
(e.Item.FindControl("txtValueInput")).Visible = false;
(e.Item.FindControl("cbField")).Visible = true;
}
if (((e.Item is GridEditableItem) && (e.Item.IsInEditMode)) && !(e.Item.DataItem is GridInsertionObject))
{
var item = (GridEditableItem) e.Item;
var mask = (WSFileNameRule) item.DataItem;
RadComboBox cbValue = (RadComboBox) item.FindControl("cbValue");
RadTextBox txtValueInput = (RadTextBox) item.FindControl("txtValueInput");
RadComboBox cbField = (RadComboBox) item.FindControl("cbField");
if (mask.TypeId == ((int)CatalogTypeEnum.LibraryType.Custom))
{
cbValue.Visible = false;
txtValueInput.Visible = true;
cbField.Visible = false;
}
else if (mask.TypeId == ((int)CatalogTypeEnum.LibraryType.Predefined))
{
cbValue.Visible = true;
txtValueInput.Visible = false;
cbField.Visible = false;
txtValueInput.Text = null;
}
else if (mask.TypeId == ((int)CatalogTypeEnum.LibraryType.Field))
{
cbValue.Visible = false;
txtValueInput.Visible = false;
cbField.Visible = true;
txtValueInput.Text = null;
}
}
}
protected void MyGrid_NeedSource(object sender, GridNeedDataSourceEventArgs e)
{
MyGrid.DataSource = (List<WSFileNameRule>) Session["FileNameRule"] ?? new List<WSFileNameRule>();
}
private void MyGrid_Updatecommand(object sender, GridCommandEventArgs e)
{
if (!Page.IsValid) return;
var item = e.Item as GridEditableItem;
var list = (List<WSFileNameRule>) Session["FileNameRule"];
if (item != null)
{
object id = item.GetDataKeyValue("Id");
WSFileNameRule mask = list.FindLast(x => x.Id == Convert.ToInt16(id));
if (mask == null)
{
list.Add(CreateObject(e, list.Count));
}
else
{
UpdateObject(e, mask, Convert.ToInt16(id));
}
}
list.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = list;
FormatExampleView(list);
MyGrid.Rebind();
}
private void MyGrid_DeleteCommand(object sender, GridCommandEventArgs e)
{
if (e.Item == null)
{
e.Canceled = true;
return;
}
object id = ((GridDataItem) e.Item).GetDataKeyValue("Id");
var list = (List<WSFileNameRule>) Session["FileNameRule"];
WSFileNameRule item = list.FindLast(x => x.Id == Convert.ToInt16(id));
if (item != null) list.Remove(item);
list.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = list;
FormatExampleView(list);
MyGrid.Rebind();
}
protected void MyGrid_InsertCommand(object sender, GridCommandEventArgs e)
{
AddValues(CreateObject(e, 0));
}
protected void MyGrid_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName != RadGrid.UpdateCommandName)
{
if (e.CommandName == RadGrid.InitInsertCommandName && e.Item.OwnerTableView.IsItemInserted)
{
e.Canceled = true;
}
if (e.CommandName == RadGrid.EditCommandName && e.Item.OwnerTableView.IsItemInserted)
{
e.Canceled = true;
}
if (e.CommandName == RadGrid.EditCommandName && e.Item.OwnerTableView.ChildEditItems.Count > 0)
{
e.Canceled = true;
}
if (e.CommandName == RadGrid.InitInsertCommandName && e.Item.OwnerTableView.ChildEditItems.Count > 0)
{
e.Canceled = true;
}
}
if (e.CommandName == RadGrid.PerformInsertCommandName)
{
var item = (GridDataItem) e.Item;
//((RadNumericTextBox)item.FindControl("txtAmount")).Enabled = fvAmmountValidate;
// ((RadNumericTextBox)item.FindControl("txtAmountTo")).Enabled = fvAmmountValidate;
// ((RadTextBox)item.FindControl("txtChar")).Enabled = fvCharValidate;
}
if (e.CommandName == RadGrid.EditCommandName)
{
e.Item.OwnerTableView.IsItemInserted = false;
}
switch (e.CommandName)
{
case RadGrid.EditCommandName:
case RadGrid.DeleteCommandName:
Session["ItemID"] = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
break;
case "SelectRow":
Session["SelectedRows"] = MyGrid.MasterTableView.GetSelectedItems();
break;
}
}
private void MyGrid_ItemCreated(object sender, GridItemEventArgs e)
{
var item = e.Item as GridCommandItem;
if (item != null)
{
LoadLabelSettings(item);
}
}
protected void MyGrid_RowDrop(object sender, GridDragDropEventArgs e)
{
if (e.DraggedItems.Count > 0 && e.DestDataItem != null)
{
var currentId = ((int) e.DraggedItems[0].GetDataKeyValue("Id"));
var destionationId = ((int) e.DestDataItem.GetDataKeyValue("Id"));
var list = (List<WSFileNameRule>) Session["FileNameRule"];
int destinationOrderIndex = list.Find(x => x.Id == destionationId).Position;
int orderIndex = list.Find(x => x.Id == currentId).Position;
if (e.DropPosition == GridItemDropPosition.Below)
{
destinationOrderIndex = destinationOrderIndex + 1;
WSFileNameRule itemNameRule =
list.Find(x => x.Position >= destinationOrderIndex && x.Id != currentId);
if (itemNameRule != null) itemNameRule.Position = itemNameRule.Position + 1;
itemNameRule = list.Find(x => x.Id == currentId);
itemNameRule.Position = destinationOrderIndex;
}
else if (e.DropPosition == GridItemDropPosition.Above)
{
destinationOrderIndex = destinationOrderIndex - 1;
WSFileNameRule itemMask = list.Find(x => x.Position <= destinationOrderIndex && x.Id != currentId);
if (itemMask != null) itemMask.Position = itemMask.Position - 1;
itemMask = list.Find(x => x.Id == currentId);
itemMask.Position = destinationOrderIndex;
}
list.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = list;
FormatExampleView(list);
MyGrid.DataSource = Session["FileNameRule"];
MyGrid.Rebind();
}
}
#endregion
protected void cbType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combo = (RadComboBox) sender;
if (Session["ItemID"] != null && Convert.ToInt16(Session["ItemID"]) > 0)
{
var item = (GridDataItem) combo.NamingContainer;
SelectedIdexChanged(item);
}
else
{
var item = (GridDataInsertItem) combo.NamingContainer;
SelectedIdexChanged(item);
}
}
public void DeleteSelectedItems(object sender, EventArgs eventArgs)
{
if (MyGrid.SelectedItems.Count > 0)
{
var RadWindowManager1 = Page.FindControl("RadWindowManager1") as RadWindowManager;
RadWindowManager1.RadConfirm(
_resources.GetString("DeleteSelectedValuesText", new CultureInfo(_culture)),
"confirmCallBackDeleteSelectedListValues", 330,
100, null, _resources.GetString("DeleteSelectedValuesTitle", new CultureInfo(_culture)));
}
}
public void DeleteValues(object sender, EventArgs eventArgs)
{
if (MyGrid.Items.Count > 0)
{
var RadWindowManager1 = Page.FindControl("RadWindowManager1") as RadWindowManager;
RadWindowManager1.RadConfirm(
_resources.GetString("DeleteAllValuesText", new CultureInfo(_culture)),
"confirmCallBackDeleteAllListValues", 330,
100, null, _resources.GetString("DeleteAllValuesTitle", new CultureInfo(_culture)));
}
}
#endregion
#region PageMethods
private void LoadAjax()
{
AjaxManager = Page.FindControl("RadAjaxManager1") as RadAjaxManager;
AjaxManager.AjaxRequest += RadAjaxManager1_AjaxRequest;
Loading = Page.FindControl("LoadCombo") as RadAjaxLoadingPanel;
if (AjaxManager == null || Loading == null) return;
AjaxManager.AjaxSettings.AddAjaxSetting(MyGrid, MyGrid, Loading);
AjaxManager.AjaxSettings.AddAjaxSetting(MyGrid, lblShowAS, Loading);
AjaxManager.AjaxSettings.AddAjaxSetting(AjaxManager, MyGrid, Loading);
}
private void GetInfo()
{
_culture = Variables.CurrentCulture();
_resources = Components.GeneralFunctions.Resources.GetAdminUserControlResources("FileNameRule", "Library");
}
private void LoadSettings()
{
try
{
GridSettings();
if (string.IsNullOrEmpty(_culture)) GetInfo();
lblShowAsTitle.Text = _resources.GetString(lblShowAsTitle.ID, new CultureInfo(_culture));
}
catch (Exception ex)
{
Exception exc = ex;
}
}
private void LoadData(List<WSFileNameRule> FileNameRules)
{
GetInfo();
foreach (WSFileNameRule rule in FileNameRules)
{
rule.Type = _resources.GetString(rule.Type, new CultureInfo(_culture)) ?? rule.Type;
if (rule.TypeId == (int)CatalogTypeEnum.LibraryType.Predefined) rule.ValueName = _resources.GetString(rule.ValueName, new CultureInfo(_culture)) ?? rule.ValueName;
}
Session["FileNameRule"] = FileNameRules;
FormatExampleView(FileNameRules);
}
private void GridSettings()
{
CustomProperties(MyGrid); /* use common properties */
GridFunction.SetHeaderName(MyGrid, _resources, null);
GridFunction.GridOptions(MyGrid);
}
private void LoadLabelSettings(GridCommandItem item)
{
var lblQuickAdd = ((Label) item.FindControl("lblQuickAdd"));
if (lblQuickAdd != null)
{
lblQuickAdd.Text = _resources.GetString(lblQuickAdd.ID, new CultureInfo(_culture));
lblQuickAdd.ToolTip = _resources.GetString(lblQuickAdd.ID, new CultureInfo(_culture));
}
var lblDeleteAllValues = ((Label) item.FindControl("lblDeleteAllValues"));
if (lblDeleteAllValues != null)
{
lblDeleteAllValues.Text = _resources.GetString(lblDeleteAllValues.ID, new CultureInfo(_culture));
lblDeleteAllValues.ToolTip = _resources.GetString(lblDeleteAllValues.ID, new CultureInfo(_culture));
}
var lblDeleteSelected = ((Label) item.FindControl("lblDeleteSelected"));
if (lblDeleteSelected != null)
{
lblDeleteSelected.Text = _resources.GetString(lblDeleteSelected.ID, new CultureInfo(_culture));
lblDeleteSelected.ToolTip = _resources.GetString(lblDeleteSelected.ID, new CultureInfo(_culture));
}
}
private void RebindGrid()
{
MyGrid.Rebind();
}
public void CustomProperties(RadGrid grid)
{
grid.AllowPaging = true;
grid.AllowSorting = true;
grid.CellSpacing = 0;
grid.EnableHeaderContextMenu = false;
grid.EnableHeaderContextFilterMenu = false;
grid.AllowFilteringByColumn = true;
grid.GridLines = GridLines.None;
grid.Skin = "Vista";
grid.Width = Unit.Percentage(100);
grid.PageSize = 10;
grid.ItemStyle.VerticalAlign = VerticalAlign.Middle;
grid.GroupingSettings.CaseSensitive = false;
grid.PagerStyle.AlwaysVisible = false;
grid.PagerStyle.Mode = GridPagerMode.NumericPages;
grid.ClientSettings.ActiveRowIndex = "true";
grid.ClientSettings.AllowColumnHide = false;
grid.ClientSettings.Resizing.AllowColumnResize = false;
grid.ClientSettings.Resizing.ClipCellContentOnResize = true;
grid.ClientSettings.Resizing.EnableRealTimeResize = true;
grid.ClientSettings.Scrolling.AllowScroll = false;
grid.ClientSettings.Scrolling.UseStaticHeaders = true;
grid.Culture = new CultureInfo(Variables.CurrentCulture());
grid.MasterTableView.GetColumnSafe("RowIndicator").Display = false;
grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
grid.MasterTableView.EditMode = GridEditMode.InPlace;
grid.MasterTableView.Width = Unit.Percentage(100);
grid.MasterTableView.EnableHeaderContextMenu = false;
}
#endregion
#region GridMethods
private void DeleteItSelectedItems()
{
var listValues = (List<WSFileNameRule>) Session["FileNameRule"];
var selectedRow = (GridDataItem[]) Session["SelectedRows"];
if (selectedRow.Length == 0)
{
return;
}
foreach (GridDataItem item in selectedRow)
{
string id = item.GetDataKeyValue("Id").ToString();
string stringMaskId = item.GetDataKeyValue("LibraryId").ToString();
listValues.Remove(listValues.Find(x => x.Id == Convert.ToInt16(id)));
}
Session["FileNameRule"] = listValues;
Session["SelectedRows"] = null;
MyGrid.Rebind();
}
private void AddValues(WSFileNameRule value)
{
var myValues = new List<WSFileNameRule>();
if (Session["FileNameRule"] != null)
{
myValues = (List<WSFileNameRule>) Session["FileNameRule"];
}
if (myValues.Count == 0)
value.Position = 1;
else
{
value.Position = myValues.Count + 1;
}
myValues.Add(value);
myValues.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = myValues;
FormatExampleView(myValues);
MyGrid.DataSource = myValues;
MyGrid.Rebind();
}
private void LoadComboType(GridItemEventArgs e)
{
var item = (GridEditableItem) e.Item;
var combo = (RadComboBox) item.FindControl("cbType");
combo.Items.Clear();
List<WSCatalogDetails> result =
new WSOrganization().GetCatalogDetail((int) CatalogTypeEnum.WS_Catalog.Library_Type);
foreach (WSCatalogDetails itemDetail in result)
{
combo.Items.Add(
new RadComboBoxItem(
_resources.GetString(itemDetail.Text, new CultureInfo(_culture)), itemDetail.Id.ToString()));
}
combo.DataBind();
if (!(e.Item is IGridInsertItem))
{
var fileNameRule = (WSFileNameRule) e.Item.DataItem;
if (fileNameRule.TypeId != 0)
combo.SelectedValue = fileNameRule.TypeId.ToString();
}
combo.EmptyMessage = _resources.GetString("cbType", new CultureInfo(_culture));
combo.DataBind();
}
private void LoadComboValue(GridItemEventArgs e)
{
var item = (GridEditableItem) e.Item;
var combo = (RadComboBox) item.FindControl("cbValue");
combo.Items.Clear();
List<WSCatalogDetails> result =
new WSOrganization().GetCatalogDetail((int) CatalogTypeEnum.WS_Catalog.Library_Type_Predefined);
foreach (WSCatalogDetails itemDetail in result)
{
combo.Items.Add(
new RadComboBoxItem(
_resources.GetString(itemDetail.Text, new CultureInfo(_culture)), itemDetail.Id.ToString()));
}
combo.DataBind();
if (!(e.Item is IGridInsertItem))
{
var fileNameRule = (WSFileNameRule) e.Item.DataItem;
if (fileNameRule.ValueId != 0)
combo.SelectedValue = fileNameRule.ValueId.ToString();
}
combo.EmptyMessage = _resources.GetString("cbValue", new CultureInfo(_culture));
combo.DataBind();
}
private void LoadComboField(GridItemEventArgs e)
{
var item = (GridEditableItem) e.Item;
var combo = (RadComboBox) item.FindControl("cbField");
combo.Items.Clear();
//TODO: ask if show all field ( mark for delete and isn't active)
List<WSField> result =
new WSOrganization().GetAllFields((int) GlobalEnumeration.QueryFor.Admin);
//foreach (WSField itemDetail in result)
//{
// combo.Items.Add(new RadComboBoxItem(itemDetail.Header, itemDetail.Id.ToString()));
//}
//combo.DataBind();
combo.DataSource = result;
combo.DataBind();
if (!(e.Item is IGridInsertItem))
{
var fileNameRule = (WSFileNameRule) e.Item.DataItem;
if (fileNameRule.FieldId != 0)
combo.SelectedValue = fileNameRule.FieldId.ToString();
}
combo.EmptyMessage = _resources.GetString("cbField", new CultureInfo(_culture));
combo.DataBind();
}
private void SelectedIdexChanged(GridDataInsertItem item)
{
var cbType = (RadComboBox) item.FindControl("cbType");
(item.FindControl("cbValue")).Visible = false;
(item.FindControl("txtValueInput")).Visible = true;
(item.FindControl("cbField")).Visible = false;
ShowControls(cbType.SelectedValue, (RadComboBox) item.FindControl("cbValue"),
(RadTextBox) item.FindControl("txtValueInput"), (RadComboBox) item.FindControl("cbField"), null);
}
private void ShowControls(string cbType, RadComboBox cbValue, RadTextBox txtValueInput, RadComboBox cbField, string value)
{
if (cbType == ((int) CatalogTypeEnum.LibraryType.Custom).ToString())
{
cbValue.Visible = false;
txtValueInput.Visible = true;
txtValueInput.Text = value;
cbField.Visible = false;
}
else if (cbType == ((int) CatalogTypeEnum.LibraryType.Predefined).ToString())
{
cbValue.Visible = true;
txtValueInput.Visible = false;
cbField.Visible = false;
txtValueInput.Text = null;
}
else if (cbType == ((int) CatalogTypeEnum.LibraryType.Field).ToString())
{
cbValue.Visible = false;
txtValueInput.Visible = false;
cbField.Visible = true;
txtValueInput.Text = null;
}
Session["TypeId"] = cbType;
}
private void SelectedIdexChanged(GridDataItem item)
{
var cbType = (RadComboBox) item.FindControl("cbType");
ShowControls(cbType.SelectedValue, (RadComboBox) item.FindControl("cbValue"),
(RadTextBox) item.FindControl("txtValueInput"), (RadComboBox) item.FindControl("cbField"), null);
}
private WSFileNameRule CreateObject(GridCommandEventArgs e, int countItem)
{
if (countItem == 0)
countItem = Session["FileNameRule"] != null ? ((List<WSFileNameRule>) Session["FileNameRule"]).Count : 0;
int LibraryId = Convert.ToInt32((Page.FindControl("hddId") as HiddenField).Value ?? "0");
string valueId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue)
? "0"
: ((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue;
string fieldId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbField")).SelectedValue)
? "0"
: ((RadComboBox) e.Item.FindControl("cbField")).SelectedValue;
var fileNameRule = new WSFileNameRule
{
Id = GetLastItemId(LibraryId),
Position = countItem + 1,
TypeId =
Convert.ToInt16(((RadComboBox) e.Item.FindControl("cbType")).SelectedValue),
ValueId = Convert.ToInt16(valueId),
Value = ((RadTextBox) e.Item.FindControl("txtValueInput")).Text,
FieldId = Convert.ToInt16(fieldId),
FieldName = ((RadComboBox) e.Item.FindControl("cbField")).Text,
Type = ((RadComboBox) e.Item.FindControl("cbType")).Text,
ValueName = ((RadComboBox) e.Item.FindControl("cbValue")).Text,
};
return fileNameRule;
}
private int GetLastItemId(Int32 libraryId)
{
int tempId = 0;
List<WSFileNameRule> myValues;
if (Session["StringData"] != null)
{
myValues = (List<WSFileNameRule>) Session["FileNameRule"];
int temp = new WSFileNameRule().GetFileNameRuleId(libraryId);
if (myValues.Count == 0)
{
myValues = new List<WSFileNameRule>();
tempId = temp != 1 && temp > 1 ? temp : 1;
}
else
{
tempId = temp != 1 && temp > myValues[myValues.Count - 1].Id
? temp
: myValues[myValues.Count - 1].Id + 1;
}
}
else
{
myValues = new List<WSFileNameRule>();
tempId = 1;
}
return tempId;
}
private void UpdateObject(GridCommandEventArgs e, WSFileNameRule fileNameRule, int Id)
{
string typeId = (Session["TypeId"] != null)
? Session["TypeId"].ToString()
: ((RadComboBox) e.Item.FindControl("cbType")).SelectedValue;
fileNameRule.Id = Id;
fileNameRule.Position = Convert.ToInt16(((Label) e.Item.FindControl("lblPosition")).Text);
fileNameRule.TypeId = Convert.ToInt16(typeId);
fileNameRule.Type = ((RadComboBox) e.Item.FindControl("cbType")).Text;
fileNameRule.ValueName = ((RadComboBox) e.Item.FindControl("cbValue")).Text;
fileNameRule.FieldName = ((RadComboBox) e.Item.FindControl("cbField")).Text;
string valueId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue)
? "0"
: (((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue);
fileNameRule.ValueId = Convert.ToInt16(valueId);
if (Convert.ToInt16(valueId) > 0)
{
fileNameRule.Value = null;
fileNameRule.FieldName = null;
}
string fieldId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbField")).SelectedValue) ? "0" :
(((RadComboBox) e.Item.FindControl("cbField")).SelectedValue)
;
fileNameRule.FieldId = Convert.ToInt16(fieldId);
if (Convert.ToInt16(fieldId) > 0)
{
fileNameRule.ValueName = null;
fileNameRule.FieldName = null;
}
fileNameRule.Value = ((RadTextBox) e.Item.FindControl("txtValueInput")).Text;
Session.Remove("TypeId");
}
private List<WSFileNameRule> GetStringMaskList()
{
List<WSFileNameRule> list = (List<WSFileNameRule>) Session["FileNameRule"] ?? new List<WSFileNameRule>();
Session["FileNameRule"] = list;
return list;
}
#region Validators
protected void cvValueInput_ServerValidate(object source, ServerValidateEventArgs args)
{
var cvValueInput = (CustomValidator) source;
Control row = cvValueInput.Parent;
var cbType = (row.FindControl("cbType") as RadComboBox);
if (cbType.SelectedValue == ((int) CatalogTypeEnum.LibraryType.Custom).ToString())
{
var txtValueInput = (row.FindControl("txtValueInput") as RadTextBox);
if (string.IsNullOrEmpty(txtValueInput.Text)) args.IsValid = false;
else args.IsValid = true;
}
else
{
args.IsValid = true;
}
}
protected void cvValue_ServerValidate(object source, ServerValidateEventArgs args)
{
var cvValue = (CustomValidator) source;
Control row = cvValue.Parent;
var cbType = (row.FindControl("cbType") as RadComboBox);
if (cbType.SelectedValue == ((int) CatalogTypeEnum.LibraryType.Predefined).ToString())
{
var cbValue = (row.FindControl("cbValue") as RadComboBox);
if (string.IsNullOrEmpty(cbValue.SelectedValue)) args.IsValid = false;
else args.IsValid = true;
}
else
{
args.IsValid = true;
}
}
protected void cvField_ServerValidate(object source, ServerValidateEventArgs args)
{
var cvField = (CustomValidator) source;
Control row = cvField.Parent;
var cbType = (row.FindControl("cbType") as RadComboBox);
if (cbType.SelectedValue == ((int) CatalogTypeEnum.LibraryType.Field).ToString())
{
var cbField = (row.FindControl("cbField") as RadComboBox);
if (string.IsNullOrEmpty(cbField.SelectedValue)) args.IsValid = false;
else args.IsValid = true;
}
else
{
args.IsValid = true;
}
}
#endregion
private void FormatExampleView(List<WSFileNameRule> list)
{
lblShowAS.Text = null;
foreach (WSFileNameRule rule in list)
{
switch (rule.TypeId)
{
case ((int)CatalogTypeEnum.LibraryType.Custom):
lblShowAS.Text += rule.Value;
break;
case ((int)CatalogTypeEnum.LibraryType.Predefined):
if (rule.ValueId == (int)CatalogTypeEnum.LibraryType_Value.FrontEnd)
lblShowAS.Text += @"\";
else
lblShowAS.Text += rule.ValueName;
break;
case ((int)CatalogTypeEnum.LibraryType.Field):
lblShowAS.Text += rule.FieldName;
break;
}
}
}
#endregion
}
i need load items ondemand, but OnItemrequest does not fire.
The grid is a usercontrol.
This is my code
-ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FileNameRule.ascx.cs"
Inherits="WebSearch.UI.Admin.UserControls.Library.FileNameRule" %>
<link href="/UI/Style/Admin/UserControls/FileNameRule.css" rel="stylesheet" type="text/css" />
<div class="exampleView">
<div class="title">
<asp:Label ID="lblShowAsTitle" runat="server" />
</div>
<div class="label">
<asp:Label ID="lblShowAS" runat="server" />
</div>
</div>
<div class="clear">
</div>
<div class="exampleView">
<div class="clear">
</div>
<telerik:RadGrid runat="server" ID="MyGrid" AutoGenerateColumns="false" Skin="Vista"
AllowMultiRowSelection="True" PageSize="10" AllowPaging="true" EnableViewState="false">
<MasterTableView DataKeyNames="LibraryId,Id" CommandItemDisplay="Top" EditMode="InPlace">
<Columns>
<telerik:GridClientSelectColumn CommandName="Select" UniqueName="Select" HeaderStyle-Width="30px"
Resizable="false" />
<telerik:GridEditCommandColumn HeaderStyle-Width="25px" UniqueName="EditCommandColumn"
ButtonType="ImageButton" EditImageUrl="/UI/Images/grid_edit.png" Resizable="false">
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn UniqueName="btnDelete" ConfirmDialogType="RadWindow" ButtonType="ImageButton"
CommandName="Delete" ConfirmDialogHeight="100px" ConfirmDialogWidth="300px" HeaderStyle-Width="25px"
Resizable="false" ImageUrl="/UI/Images/cross.png" />
<telerik:GridTemplateColumn DataField="Id" HeaderText="Id" UniqueName="Id" Visible="false"
ConvertEmptyStringToNull="true">
<InsertItemTemplate>
<telerik:RadTextBox ID="lblId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %> '
Width="150px" ReadOnly="true" Enabled="false" CssClass="labelGrid" />
</InsertItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %> '
Width="150px" CssClass="labelGrid" ReadOnly="true" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="Position" HeaderText="Position" UniqueName="Position" Visible="false" HeaderStyle-Width="1px"
ConvertEmptyStringToNull="true">
<EditItemTemplate>
<asp:Label ID="lblPosition" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Position") %> '
Width="150px" CssClass="labelGrid" ReadOnly="true" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Position") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Type" DataField="TypeId">
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="cbType" EnableLoadOnDemand="True" EnableAutomaticLoadOnDemand="true" DataTextField="CDetailText"
DataValueField="CDetailId" HighlightTemplatedItems="true" Width="90%" CausesValidation="false" AutoPostBack="true" OnSelectedIndexChanged="cbType_SelectedIndexChanged"/>
<br />
<asp:RequiredFieldValidator ID="frSecurity" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="cbType"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDatatype" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Type") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="Value" DataField="ValueId">
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="cbValue" EnableLoadOnDemand="True" DataTextField="CDetailText"
DataValueField="CDetailId" HighlightTemplatedItems="true" Width="90%" CausesValidation="false" />
<telerik:RadComboBox runat="server" ID="cbField" DataTextField="Header"
DataValueField="Id" HighlightTemplatedItems="true" Width="90%" CausesValidation="false" ItemsPerRequest="10"
ShowMoreResultsBox="true" EnableVirtualScrolling="true" EnableLoadOnDemand="True" />
<telerik:RadTextBox runat="server" ID="txtValueInput"></telerik:RadTextBox>
<br />
<asp:CustomValidator ID="cvValue" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="cbValue" OnServerValidate="cvValue_ServerValidate"></asp:CustomValidator>
<asp:CustomValidator ID="cvField" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="cbField" OnServerValidate="cvField_ServerValidate"></asp:CustomValidator>
<asp:CustomValidator ID="cvValueInput" runat="server" ErrorMessage="*" CssClass="validator"
Display="Dynamic" ControlToValidate="txtValueInput" OnServerValidate="cvValueInput_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblValue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ValueName") %>'></asp:Label>
<asp:Label ID="lblFieldName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FieldName") %>'></asp:Label>
<asp:Label ID="lbValueInput" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Value") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<CommandItemTemplate>
<div class="quickAdd">
<asp:LinkButton ID="lnkqQuitInsert" runat="server" CommandName="InitInsert">
<asp:ImageButton ID="imgQuickAdd" runat="server" ImageUrl="/UI/Images/add.png" />
<asp:Label ID="lblQuickAdd" runat="server" /></asp:LinkButton>
<asp:LinkButton ID="lnkDeleteSelected" CommandName="SelectRow" runat="server" OnClick="DeleteSelectedItems">
<asp:ImageButton ID="imgDeleteSelected" runat="server" ImageUrl="/UI/Images/cross.png" />
<asp:Label ID="lblDeleteSelected" runat="server" /></asp:LinkButton>
<asp:LinkButton ID="lnkDeleteAllValues" runat="server" OnClick="DeleteValues">
<asp:ImageButton ID="imgDeleteAllValues" runat="server" ImageUrl="/UI/Images/delete_all.png" />
<asp:Label ID="lblDeleteAllValues" runat="server" /></asp:LinkButton>
</div>
</CommandItemTemplate>
</MasterTableView>
<ClientSettings AllowRowsDragDrop="True">
<Selecting AllowRowSelect="True" EnableDragToSelectRows="false" />
<ClientEvents OnGridCreated="GridCreated" />
</ClientSettings>
<PagerStyle Mode="NumericPages" />
</telerik:RadGrid>
</div>
.ascx.cs
public partial class FileNameRule : UserControl
{
#region Properties&Variables
private string _culture;
private ResourceManager _resources;
private RadAjaxManager AjaxManager { get; set; }
private RadAjaxLoadingPanel Loading { get; set; }
public List<WSFileNameRule> GetFileNameRuleList
{
get { return GetStringMaskList(); }
set { LoadData(value); }
}
#endregion
#region Events
protected void Page_Load(object sender, EventArgs e)
{
LoadAjax();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
GetInfo();
LoadSettings();
MyGrid.NeedDataSource += MyGrid_NeedSource;
MyGrid.InsertCommand += MyGrid_InsertCommand;
MyGrid.ItemCommand += MyGrid_ItemCommand;
MyGrid.DeleteCommand += MyGrid_DeleteCommand;
MyGrid.UpdateCommand += MyGrid_Updatecommand;
MyGrid.ItemCreated += MyGrid_ItemCreated;
MyGrid.ItemDataBound += MyGrid_ItemDataBound;
MyGrid.RowDrop += MyGrid_RowDrop;
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
switch (e.Argument)
{
case "RebindAndNavigate":
RebindGrid();
break;
case "DeleteAll":
Session["FileNameRule"] = null;
MyGrid.DataSource = new List<WSFileNameRule>();
MyGrid.Rebind();
break;
case "DeleteSelected":
DeleteItSelectedItems();
break;
}
}
#endregion
#region Methods
#region MyGridEvents
protected void MyGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode || (e.Item is IGridInsertItem))
{
LoadComboType(e);
LoadComboValue(e);
LoadComboField(e);
(e.Item.FindControl("cbValue")).Visible = false;
(e.Item.FindControl("txtValueInput")).Visible = false;
(e.Item.FindControl("cbField")).Visible = true;
}
if (((e.Item is GridEditableItem) && (e.Item.IsInEditMode)) && !(e.Item.DataItem is GridInsertionObject))
{
var item = (GridEditableItem) e.Item;
var mask = (WSFileNameRule) item.DataItem;
RadComboBox cbValue = (RadComboBox) item.FindControl("cbValue");
RadTextBox txtValueInput = (RadTextBox) item.FindControl("txtValueInput");
RadComboBox cbField = (RadComboBox) item.FindControl("cbField");
if (mask.TypeId == ((int)CatalogTypeEnum.LibraryType.Custom))
{
cbValue.Visible = false;
txtValueInput.Visible = true;
cbField.Visible = false;
}
else if (mask.TypeId == ((int)CatalogTypeEnum.LibraryType.Predefined))
{
cbValue.Visible = true;
txtValueInput.Visible = false;
cbField.Visible = false;
txtValueInput.Text = null;
}
else if (mask.TypeId == ((int)CatalogTypeEnum.LibraryType.Field))
{
cbValue.Visible = false;
txtValueInput.Visible = false;
cbField.Visible = true;
txtValueInput.Text = null;
}
}
}
protected void MyGrid_NeedSource(object sender, GridNeedDataSourceEventArgs e)
{
MyGrid.DataSource = (List<WSFileNameRule>) Session["FileNameRule"] ?? new List<WSFileNameRule>();
}
private void MyGrid_Updatecommand(object sender, GridCommandEventArgs e)
{
if (!Page.IsValid) return;
var item = e.Item as GridEditableItem;
var list = (List<WSFileNameRule>) Session["FileNameRule"];
if (item != null)
{
object id = item.GetDataKeyValue("Id");
WSFileNameRule mask = list.FindLast(x => x.Id == Convert.ToInt16(id));
if (mask == null)
{
list.Add(CreateObject(e, list.Count));
}
else
{
UpdateObject(e, mask, Convert.ToInt16(id));
}
}
list.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = list;
FormatExampleView(list);
MyGrid.Rebind();
}
private void MyGrid_DeleteCommand(object sender, GridCommandEventArgs e)
{
if (e.Item == null)
{
e.Canceled = true;
return;
}
object id = ((GridDataItem) e.Item).GetDataKeyValue("Id");
var list = (List<WSFileNameRule>) Session["FileNameRule"];
WSFileNameRule item = list.FindLast(x => x.Id == Convert.ToInt16(id));
if (item != null) list.Remove(item);
list.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = list;
FormatExampleView(list);
MyGrid.Rebind();
}
protected void MyGrid_InsertCommand(object sender, GridCommandEventArgs e)
{
AddValues(CreateObject(e, 0));
}
protected void MyGrid_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName != RadGrid.UpdateCommandName)
{
if (e.CommandName == RadGrid.InitInsertCommandName && e.Item.OwnerTableView.IsItemInserted)
{
e.Canceled = true;
}
if (e.CommandName == RadGrid.EditCommandName && e.Item.OwnerTableView.IsItemInserted)
{
e.Canceled = true;
}
if (e.CommandName == RadGrid.EditCommandName && e.Item.OwnerTableView.ChildEditItems.Count > 0)
{
e.Canceled = true;
}
if (e.CommandName == RadGrid.InitInsertCommandName && e.Item.OwnerTableView.ChildEditItems.Count > 0)
{
e.Canceled = true;
}
}
if (e.CommandName == RadGrid.PerformInsertCommandName)
{
var item = (GridDataItem) e.Item;
//((RadNumericTextBox)item.FindControl("txtAmount")).Enabled = fvAmmountValidate;
// ((RadNumericTextBox)item.FindControl("txtAmountTo")).Enabled = fvAmmountValidate;
// ((RadTextBox)item.FindControl("txtChar")).Enabled = fvCharValidate;
}
if (e.CommandName == RadGrid.EditCommandName)
{
e.Item.OwnerTableView.IsItemInserted = false;
}
switch (e.CommandName)
{
case RadGrid.EditCommandName:
case RadGrid.DeleteCommandName:
Session["ItemID"] = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
break;
case "SelectRow":
Session["SelectedRows"] = MyGrid.MasterTableView.GetSelectedItems();
break;
}
}
private void MyGrid_ItemCreated(object sender, GridItemEventArgs e)
{
var item = e.Item as GridCommandItem;
if (item != null)
{
LoadLabelSettings(item);
}
}
protected void MyGrid_RowDrop(object sender, GridDragDropEventArgs e)
{
if (e.DraggedItems.Count > 0 && e.DestDataItem != null)
{
var currentId = ((int) e.DraggedItems[0].GetDataKeyValue("Id"));
var destionationId = ((int) e.DestDataItem.GetDataKeyValue("Id"));
var list = (List<WSFileNameRule>) Session["FileNameRule"];
int destinationOrderIndex = list.Find(x => x.Id == destionationId).Position;
int orderIndex = list.Find(x => x.Id == currentId).Position;
if (e.DropPosition == GridItemDropPosition.Below)
{
destinationOrderIndex = destinationOrderIndex + 1;
WSFileNameRule itemNameRule =
list.Find(x => x.Position >= destinationOrderIndex && x.Id != currentId);
if (itemNameRule != null) itemNameRule.Position = itemNameRule.Position + 1;
itemNameRule = list.Find(x => x.Id == currentId);
itemNameRule.Position = destinationOrderIndex;
}
else if (e.DropPosition == GridItemDropPosition.Above)
{
destinationOrderIndex = destinationOrderIndex - 1;
WSFileNameRule itemMask = list.Find(x => x.Position <= destinationOrderIndex && x.Id != currentId);
if (itemMask != null) itemMask.Position = itemMask.Position - 1;
itemMask = list.Find(x => x.Id == currentId);
itemMask.Position = destinationOrderIndex;
}
list.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = list;
FormatExampleView(list);
MyGrid.DataSource = Session["FileNameRule"];
MyGrid.Rebind();
}
}
#endregion
protected void cbType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combo = (RadComboBox) sender;
if (Session["ItemID"] != null && Convert.ToInt16(Session["ItemID"]) > 0)
{
var item = (GridDataItem) combo.NamingContainer;
SelectedIdexChanged(item);
}
else
{
var item = (GridDataInsertItem) combo.NamingContainer;
SelectedIdexChanged(item);
}
}
public void DeleteSelectedItems(object sender, EventArgs eventArgs)
{
if (MyGrid.SelectedItems.Count > 0)
{
var RadWindowManager1 = Page.FindControl("RadWindowManager1") as RadWindowManager;
RadWindowManager1.RadConfirm(
_resources.GetString("DeleteSelectedValuesText", new CultureInfo(_culture)),
"confirmCallBackDeleteSelectedListValues", 330,
100, null, _resources.GetString("DeleteSelectedValuesTitle", new CultureInfo(_culture)));
}
}
public void DeleteValues(object sender, EventArgs eventArgs)
{
if (MyGrid.Items.Count > 0)
{
var RadWindowManager1 = Page.FindControl("RadWindowManager1") as RadWindowManager;
RadWindowManager1.RadConfirm(
_resources.GetString("DeleteAllValuesText", new CultureInfo(_culture)),
"confirmCallBackDeleteAllListValues", 330,
100, null, _resources.GetString("DeleteAllValuesTitle", new CultureInfo(_culture)));
}
}
#endregion
#region PageMethods
private void LoadAjax()
{
AjaxManager = Page.FindControl("RadAjaxManager1") as RadAjaxManager;
AjaxManager.AjaxRequest += RadAjaxManager1_AjaxRequest;
Loading = Page.FindControl("LoadCombo") as RadAjaxLoadingPanel;
if (AjaxManager == null || Loading == null) return;
AjaxManager.AjaxSettings.AddAjaxSetting(MyGrid, MyGrid, Loading);
AjaxManager.AjaxSettings.AddAjaxSetting(MyGrid, lblShowAS, Loading);
AjaxManager.AjaxSettings.AddAjaxSetting(AjaxManager, MyGrid, Loading);
}
private void GetInfo()
{
_culture = Variables.CurrentCulture();
_resources = Components.GeneralFunctions.Resources.GetAdminUserControlResources("FileNameRule", "Library");
}
private void LoadSettings()
{
try
{
GridSettings();
if (string.IsNullOrEmpty(_culture)) GetInfo();
lblShowAsTitle.Text = _resources.GetString(lblShowAsTitle.ID, new CultureInfo(_culture));
}
catch (Exception ex)
{
Exception exc = ex;
}
}
private void LoadData(List<WSFileNameRule> FileNameRules)
{
GetInfo();
foreach (WSFileNameRule rule in FileNameRules)
{
rule.Type = _resources.GetString(rule.Type, new CultureInfo(_culture)) ?? rule.Type;
if (rule.TypeId == (int)CatalogTypeEnum.LibraryType.Predefined) rule.ValueName = _resources.GetString(rule.ValueName, new CultureInfo(_culture)) ?? rule.ValueName;
}
Session["FileNameRule"] = FileNameRules;
FormatExampleView(FileNameRules);
}
private void GridSettings()
{
CustomProperties(MyGrid); /* use common properties */
GridFunction.SetHeaderName(MyGrid, _resources, null);
GridFunction.GridOptions(MyGrid);
}
private void LoadLabelSettings(GridCommandItem item)
{
var lblQuickAdd = ((Label) item.FindControl("lblQuickAdd"));
if (lblQuickAdd != null)
{
lblQuickAdd.Text = _resources.GetString(lblQuickAdd.ID, new CultureInfo(_culture));
lblQuickAdd.ToolTip = _resources.GetString(lblQuickAdd.ID, new CultureInfo(_culture));
}
var lblDeleteAllValues = ((Label) item.FindControl("lblDeleteAllValues"));
if (lblDeleteAllValues != null)
{
lblDeleteAllValues.Text = _resources.GetString(lblDeleteAllValues.ID, new CultureInfo(_culture));
lblDeleteAllValues.ToolTip = _resources.GetString(lblDeleteAllValues.ID, new CultureInfo(_culture));
}
var lblDeleteSelected = ((Label) item.FindControl("lblDeleteSelected"));
if (lblDeleteSelected != null)
{
lblDeleteSelected.Text = _resources.GetString(lblDeleteSelected.ID, new CultureInfo(_culture));
lblDeleteSelected.ToolTip = _resources.GetString(lblDeleteSelected.ID, new CultureInfo(_culture));
}
}
private void RebindGrid()
{
MyGrid.Rebind();
}
public void CustomProperties(RadGrid grid)
{
grid.AllowPaging = true;
grid.AllowSorting = true;
grid.CellSpacing = 0;
grid.EnableHeaderContextMenu = false;
grid.EnableHeaderContextFilterMenu = false;
grid.AllowFilteringByColumn = true;
grid.GridLines = GridLines.None;
grid.Skin = "Vista";
grid.Width = Unit.Percentage(100);
grid.PageSize = 10;
grid.ItemStyle.VerticalAlign = VerticalAlign.Middle;
grid.GroupingSettings.CaseSensitive = false;
grid.PagerStyle.AlwaysVisible = false;
grid.PagerStyle.Mode = GridPagerMode.NumericPages;
grid.ClientSettings.ActiveRowIndex = "true";
grid.ClientSettings.AllowColumnHide = false;
grid.ClientSettings.Resizing.AllowColumnResize = false;
grid.ClientSettings.Resizing.ClipCellContentOnResize = true;
grid.ClientSettings.Resizing.EnableRealTimeResize = true;
grid.ClientSettings.Scrolling.AllowScroll = false;
grid.ClientSettings.Scrolling.UseStaticHeaders = true;
grid.Culture = new CultureInfo(Variables.CurrentCulture());
grid.MasterTableView.GetColumnSafe("RowIndicator").Display = false;
grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
grid.MasterTableView.EditMode = GridEditMode.InPlace;
grid.MasterTableView.Width = Unit.Percentage(100);
grid.MasterTableView.EnableHeaderContextMenu = false;
}
#endregion
#region GridMethods
private void DeleteItSelectedItems()
{
var listValues = (List<WSFileNameRule>) Session["FileNameRule"];
var selectedRow = (GridDataItem[]) Session["SelectedRows"];
if (selectedRow.Length == 0)
{
return;
}
foreach (GridDataItem item in selectedRow)
{
string id = item.GetDataKeyValue("Id").ToString();
string stringMaskId = item.GetDataKeyValue("LibraryId").ToString();
listValues.Remove(listValues.Find(x => x.Id == Convert.ToInt16(id)));
}
Session["FileNameRule"] = listValues;
Session["SelectedRows"] = null;
MyGrid.Rebind();
}
private void AddValues(WSFileNameRule value)
{
var myValues = new List<WSFileNameRule>();
if (Session["FileNameRule"] != null)
{
myValues = (List<WSFileNameRule>) Session["FileNameRule"];
}
if (myValues.Count == 0)
value.Position = 1;
else
{
value.Position = myValues.Count + 1;
}
myValues.Add(value);
myValues.Sort((x, y) => x.Position.CompareTo(y.Position));
Session["FileNameRule"] = myValues;
FormatExampleView(myValues);
MyGrid.DataSource = myValues;
MyGrid.Rebind();
}
private void LoadComboType(GridItemEventArgs e)
{
var item = (GridEditableItem) e.Item;
var combo = (RadComboBox) item.FindControl("cbType");
combo.Items.Clear();
List<WSCatalogDetails> result =
new WSOrganization().GetCatalogDetail((int) CatalogTypeEnum.WS_Catalog.Library_Type);
foreach (WSCatalogDetails itemDetail in result)
{
combo.Items.Add(
new RadComboBoxItem(
_resources.GetString(itemDetail.Text, new CultureInfo(_culture)), itemDetail.Id.ToString()));
}
combo.DataBind();
if (!(e.Item is IGridInsertItem))
{
var fileNameRule = (WSFileNameRule) e.Item.DataItem;
if (fileNameRule.TypeId != 0)
combo.SelectedValue = fileNameRule.TypeId.ToString();
}
combo.EmptyMessage = _resources.GetString("cbType", new CultureInfo(_culture));
combo.DataBind();
}
private void LoadComboValue(GridItemEventArgs e)
{
var item = (GridEditableItem) e.Item;
var combo = (RadComboBox) item.FindControl("cbValue");
combo.Items.Clear();
List<WSCatalogDetails> result =
new WSOrganization().GetCatalogDetail((int) CatalogTypeEnum.WS_Catalog.Library_Type_Predefined);
foreach (WSCatalogDetails itemDetail in result)
{
combo.Items.Add(
new RadComboBoxItem(
_resources.GetString(itemDetail.Text, new CultureInfo(_culture)), itemDetail.Id.ToString()));
}
combo.DataBind();
if (!(e.Item is IGridInsertItem))
{
var fileNameRule = (WSFileNameRule) e.Item.DataItem;
if (fileNameRule.ValueId != 0)
combo.SelectedValue = fileNameRule.ValueId.ToString();
}
combo.EmptyMessage = _resources.GetString("cbValue", new CultureInfo(_culture));
combo.DataBind();
}
private void LoadComboField(GridItemEventArgs e)
{
var item = (GridEditableItem) e.Item;
var combo = (RadComboBox) item.FindControl("cbField");
combo.Items.Clear();
//TODO: ask if show all field ( mark for delete and isn't active)
List<WSField> result =
new WSOrganization().GetAllFields((int) GlobalEnumeration.QueryFor.Admin);
//foreach (WSField itemDetail in result)
//{
// combo.Items.Add(new RadComboBoxItem(itemDetail.Header, itemDetail.Id.ToString()));
//}
//combo.DataBind();
combo.DataSource = result;
combo.DataBind();
if (!(e.Item is IGridInsertItem))
{
var fileNameRule = (WSFileNameRule) e.Item.DataItem;
if (fileNameRule.FieldId != 0)
combo.SelectedValue = fileNameRule.FieldId.ToString();
}
combo.EmptyMessage = _resources.GetString("cbField", new CultureInfo(_culture));
combo.DataBind();
}
private void SelectedIdexChanged(GridDataInsertItem item)
{
var cbType = (RadComboBox) item.FindControl("cbType");
(item.FindControl("cbValue")).Visible = false;
(item.FindControl("txtValueInput")).Visible = true;
(item.FindControl("cbField")).Visible = false;
ShowControls(cbType.SelectedValue, (RadComboBox) item.FindControl("cbValue"),
(RadTextBox) item.FindControl("txtValueInput"), (RadComboBox) item.FindControl("cbField"), null);
}
private void ShowControls(string cbType, RadComboBox cbValue, RadTextBox txtValueInput, RadComboBox cbField, string value)
{
if (cbType == ((int) CatalogTypeEnum.LibraryType.Custom).ToString())
{
cbValue.Visible = false;
txtValueInput.Visible = true;
txtValueInput.Text = value;
cbField.Visible = false;
}
else if (cbType == ((int) CatalogTypeEnum.LibraryType.Predefined).ToString())
{
cbValue.Visible = true;
txtValueInput.Visible = false;
cbField.Visible = false;
txtValueInput.Text = null;
}
else if (cbType == ((int) CatalogTypeEnum.LibraryType.Field).ToString())
{
cbValue.Visible = false;
txtValueInput.Visible = false;
cbField.Visible = true;
txtValueInput.Text = null;
}
Session["TypeId"] = cbType;
}
private void SelectedIdexChanged(GridDataItem item)
{
var cbType = (RadComboBox) item.FindControl("cbType");
ShowControls(cbType.SelectedValue, (RadComboBox) item.FindControl("cbValue"),
(RadTextBox) item.FindControl("txtValueInput"), (RadComboBox) item.FindControl("cbField"), null);
}
private WSFileNameRule CreateObject(GridCommandEventArgs e, int countItem)
{
if (countItem == 0)
countItem = Session["FileNameRule"] != null ? ((List<WSFileNameRule>) Session["FileNameRule"]).Count : 0;
int LibraryId = Convert.ToInt32((Page.FindControl("hddId") as HiddenField).Value ?? "0");
string valueId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue)
? "0"
: ((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue;
string fieldId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbField")).SelectedValue)
? "0"
: ((RadComboBox) e.Item.FindControl("cbField")).SelectedValue;
var fileNameRule = new WSFileNameRule
{
Id = GetLastItemId(LibraryId),
Position = countItem + 1,
TypeId =
Convert.ToInt16(((RadComboBox) e.Item.FindControl("cbType")).SelectedValue),
ValueId = Convert.ToInt16(valueId),
Value = ((RadTextBox) e.Item.FindControl("txtValueInput")).Text,
FieldId = Convert.ToInt16(fieldId),
FieldName = ((RadComboBox) e.Item.FindControl("cbField")).Text,
Type = ((RadComboBox) e.Item.FindControl("cbType")).Text,
ValueName = ((RadComboBox) e.Item.FindControl("cbValue")).Text,
};
return fileNameRule;
}
private int GetLastItemId(Int32 libraryId)
{
int tempId = 0;
List<WSFileNameRule> myValues;
if (Session["StringData"] != null)
{
myValues = (List<WSFileNameRule>) Session["FileNameRule"];
int temp = new WSFileNameRule().GetFileNameRuleId(libraryId);
if (myValues.Count == 0)
{
myValues = new List<WSFileNameRule>();
tempId = temp != 1 && temp > 1 ? temp : 1;
}
else
{
tempId = temp != 1 && temp > myValues[myValues.Count - 1].Id
? temp
: myValues[myValues.Count - 1].Id + 1;
}
}
else
{
myValues = new List<WSFileNameRule>();
tempId = 1;
}
return tempId;
}
private void UpdateObject(GridCommandEventArgs e, WSFileNameRule fileNameRule, int Id)
{
string typeId = (Session["TypeId"] != null)
? Session["TypeId"].ToString()
: ((RadComboBox) e.Item.FindControl("cbType")).SelectedValue;
fileNameRule.Id = Id;
fileNameRule.Position = Convert.ToInt16(((Label) e.Item.FindControl("lblPosition")).Text);
fileNameRule.TypeId = Convert.ToInt16(typeId);
fileNameRule.Type = ((RadComboBox) e.Item.FindControl("cbType")).Text;
fileNameRule.ValueName = ((RadComboBox) e.Item.FindControl("cbValue")).Text;
fileNameRule.FieldName = ((RadComboBox) e.Item.FindControl("cbField")).Text;
string valueId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue)
? "0"
: (((RadComboBox) e.Item.FindControl("cbValue")).SelectedValue);
fileNameRule.ValueId = Convert.ToInt16(valueId);
if (Convert.ToInt16(valueId) > 0)
{
fileNameRule.Value = null;
fileNameRule.FieldName = null;
}
string fieldId = string.IsNullOrEmpty(((RadComboBox) e.Item.FindControl("cbField")).SelectedValue) ? "0" :
(((RadComboBox) e.Item.FindControl("cbField")).SelectedValue)
;
fileNameRule.FieldId = Convert.ToInt16(fieldId);
if (Convert.ToInt16(fieldId) > 0)
{
fileNameRule.ValueName = null;
fileNameRule.FieldName = null;
}
fileNameRule.Value = ((RadTextBox) e.Item.FindControl("txtValueInput")).Text;
Session.Remove("TypeId");
}
private List<WSFileNameRule> GetStringMaskList()
{
List<WSFileNameRule> list = (List<WSFileNameRule>) Session["FileNameRule"] ?? new List<WSFileNameRule>();
Session["FileNameRule"] = list;
return list;
}
#region Validators
protected void cvValueInput_ServerValidate(object source, ServerValidateEventArgs args)
{
var cvValueInput = (CustomValidator) source;
Control row = cvValueInput.Parent;
var cbType = (row.FindControl("cbType") as RadComboBox);
if (cbType.SelectedValue == ((int) CatalogTypeEnum.LibraryType.Custom).ToString())
{
var txtValueInput = (row.FindControl("txtValueInput") as RadTextBox);
if (string.IsNullOrEmpty(txtValueInput.Text)) args.IsValid = false;
else args.IsValid = true;
}
else
{
args.IsValid = true;
}
}
protected void cvValue_ServerValidate(object source, ServerValidateEventArgs args)
{
var cvValue = (CustomValidator) source;
Control row = cvValue.Parent;
var cbType = (row.FindControl("cbType") as RadComboBox);
if (cbType.SelectedValue == ((int) CatalogTypeEnum.LibraryType.Predefined).ToString())
{
var cbValue = (row.FindControl("cbValue") as RadComboBox);
if (string.IsNullOrEmpty(cbValue.SelectedValue)) args.IsValid = false;
else args.IsValid = true;
}
else
{
args.IsValid = true;
}
}
protected void cvField_ServerValidate(object source, ServerValidateEventArgs args)
{
var cvField = (CustomValidator) source;
Control row = cvField.Parent;
var cbType = (row.FindControl("cbType") as RadComboBox);
if (cbType.SelectedValue == ((int) CatalogTypeEnum.LibraryType.Field).ToString())
{
var cbField = (row.FindControl("cbField") as RadComboBox);
if (string.IsNullOrEmpty(cbField.SelectedValue)) args.IsValid = false;
else args.IsValid = true;
}
else
{
args.IsValid = true;
}
}
#endregion
private void FormatExampleView(List<WSFileNameRule> list)
{
lblShowAS.Text = null;
foreach (WSFileNameRule rule in list)
{
switch (rule.TypeId)
{
case ((int)CatalogTypeEnum.LibraryType.Custom):
lblShowAS.Text += rule.Value;
break;
case ((int)CatalogTypeEnum.LibraryType.Predefined):
if (rule.ValueId == (int)CatalogTypeEnum.LibraryType_Value.FrontEnd)
lblShowAS.Text += @"\";
else
lblShowAS.Text += rule.ValueName;
break;
case ((int)CatalogTypeEnum.LibraryType.Field):
lblShowAS.Text += rule.FieldName;
break;
}
}
}
#endregion
}