protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
// Add validators
if ((e.Item is GridEditableItem && e.Item.IsInEditMode))
{
string validationGroup = null;
if (e.Item.ItemIndex == -1) // InsertItem
{
validationGroup = "RadGrid1_Insert";
var controls = e.Item.FindRecursive<ImageButton>();
foreach (var item in controls)
{
item.ValidationGroup = validationGroup;
}
}
else // EditItem
{
validationGroup = "RadGrid1";
}
// Add you validators here
}
}
public static class UserControlExtensions
{
public static IEnumerable<TCtrl> FindRecursive<TCtrl>(this Control control)
where TCtrl : Control
{
List<TCtrl> foundControls = new List<TCtrl>();
foreach (Control foundCtrl in control.Controls)
{
if (foundCtrl is TCtrl)
{
foundControls.Add(foundCtrl as TCtrl);
}
foundControls.AddRange(FindRecursive<TCtrl>(foundCtrl));
}
return foundControls;
}
}
Thanks to Mats Lycken for helping me on this!
Friday, 15 March 2013
RadGrid validation in both Insert mode and edit mode
At work we recently had a case where a RadGrid had to be in both Edit mode and Insert mode at the same time.
Some fields were Required and had regular expression validators that in insert mode had default value of empty string (now allowed on save)
When one was supposed to edit a row this was not possible due to the fact that the aforementioned validator would kick in and RadGrid only allows one Validation group in it's validation settings.
Solution:
In ItemCreated event we put in a check to see whether e.Item.ItemIndex was -1 (Insert item) and changed the added validators validation group for the insert Items and then use a recursive method to find all ImageButtons on this Item to change that buttons validation group to the insert validation group.
Etiketter:
EditMode,
InPlace,
InsertMode,
RadGrid,
Validation
Subscribe to:
Posts (Atom)