This is a migrated thread and some comments may be shown as answers.

RadInputManager Validation Groups in RadGrid Hierarchy

4 Answers 44 Views
Input
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 2
Andy asked on 13 Mar 2015, 05:17 PM
I have a RadGrid with hierarchy, and each DetailTable is always editable.  I'm looking for a way to have the group of input controls in the DetailTable (that have been dynamically added to a TextBoxSetting's TargetControls) to be validated in groups. The issue is that if a input in any DetailTable is in an error state, I can't update any of the DetailTables.  I've attempted using Validation Groups, but setting them in the ItemDataBound event is much too late in the page event cycle. 

In this example, I have a list of contacts in my MasterTableView. In the DetailTables, there are inputs that have been dynamically added to the RadInputManager. Can anyone think of a way to allow one DetailTable to be updated (using the btnSubmit button) when another DetailTable has an input that is in an error state?

<telerik:RadInputManager ID="inputManager" runat="server">       
    <telerik:TextBoxSetting BehaviorID="txt" Validation-IsRequired="true" />
</telerik:RadInputManager>
     
<telerik:RadGrid ID="rgUsers" runat="server" AutoGenerateColumns="false">
    <MasterTableView Name="Users" DataKeyNames="ContactId" HierarchyLoadMode="Client">
        <Columns>
            <telerik:GridBoundColumn DataField="ContactId" HeaderText="ID" />
            <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" />
            <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" />
        </Columns>
        <ParentTableRelation>
            <telerik:GridRelationFields MasterKeyField="ContactId" DetailKeyField="ContactId" />               
        </ParentTableRelation>
        <DetailTables>
            <telerik:GridTableView Name="FieldLevelChanges" DataKeyNames="ContactId" CommandItemDisplay="Bottom">
                <CommandItemTemplate>
                    <asp:Button ID="btnSubmit" runat="server" Text="Update" CommandName="Update">
                    </asp:Button>
                </CommandItemTemplate>
                <Columns>
                    <rad:GridTemplateColumn HeaderText="Value">
                        <ItemTemplate>
                            <asp:TextBox ID="txtValue" runat="server" Text='<%#Eval("Value")%>' />
                        </ItemTemplate>  
                    </rad:GridTemplateColumn>
                </Columns>                  
            </telerik:GridTableView>                               
        </DetailTables>                   
    </MasterTableView>
</telerik:RadGrid>

Protected Sub rgUsers_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles rgUsers.ItemDataBound
    If TypeOf e.Item Is GridDataItem And e.Item.OwnerTableView.Name = "FieldLevelChanges" Then
        Dim txtValue As TextBox = DirectCast(e.Item.FindControl("txtValue"), TextBox)
        inputManager.GetSettingByBehaviorID("txt").TargetControls.Add(New TargetInput(txtValue.UniqueID, True))
    End If
End Sub





4 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 18 Mar 2015, 11:28 AM
Hi,

In order to achieve the desired goal one can create a TextBoxSetting for each level in the grid hierarchy. In attachments you can find a sample web site which illustrates a possible realization of this approach. Please examine the attachment and let us know if additional questions arise.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Andy
Top achievements
Rank 2
answered on 24 Mar 2015, 01:18 PM
Only one level of the grid hierarchy is in edit mode - the second level. The issue is that every DetailTable in the second level is in edit mode, and each DetailTable needs its own TextBoxSetting in order to validate separately from each other DetailTable. I'm still not sure how to handle this situation. 
0
Angel Petrov
Telerik team
answered on 27 Mar 2015, 08:09 AM
Hi Andy,

If only the second level needs to be validated and you have more than one detail able inside it you can use the same approach(using a different setting for the GridTableView) but slightly change the code. Attached you can find a modified version of the previously provided sample that integrates such a solution.

Regards,
Angel Petrov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Andy
Top achievements
Rank 2
answered on 27 Mar 2015, 02:21 PM
Hi Angel,

I'm afraid you're still not understanding the scenario. I was able to get a very good workaround by submitting a ticket. Konstantin Dikov provided a solution that I'll include below for anyone else trying to accomplish this:
___________________________________

Using the required validation of the InputSetting in the RadInputManager will not allow you to achieve the desired behavior, even if you specify ValidationGroup to the TextBox controls and the update Button.

I have played around with your requirement and the only solution that I have found is to use RequiredFieldValidator for the TextBox control and set the ValidationGroup property to the validator, the TextBox control and the Button (with different value for each detail table).

For your convenience, following is the code that is working correctly on my end:
 
<telerik:RadInputManager ID="inputManager" runat="server">
    <telerik:TextBoxSetting BehaviorID="txt" />
</telerik:RadInputManager>
  
<telerik:RadGrid ID="rgUsers" runat="server" AutoGenerateColumns="false" OnNeedDataSource="rgUsers_NeedDataSource" OnDetailTableDataBind="rgUsers_DetailTableDataBind"
    OnItemDataBound="rgUsers_ItemDataBound">
    <MasterTableView Name="Users" DataKeyNames="ContactId" HierarchyLoadMode="Client">
        <Columns>
            <telerik:GridBoundColumn DataField="ContactId" HeaderText="ID" />
            <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" />
            <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" />
        </Columns>
        <ParentTableRelation>
            <telerik:GridRelationFields MasterKeyField="ContactId" DetailKeyField="ContactId" />
        </ParentTableRelation>
        <DetailTables>
            <telerik:GridTableView Name="FieldLevelChanges" DataKeyNames="ContactId" CommandItemDisplay="Bottom">
                <CommandItemTemplate>
                    <asp:Button ID="btnSubmit" runat="server" Text="Update" CommandName="Update" CausesValidation="true"></asp:Button>
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="Value">
                        <ItemTemplate>
                            <asp:TextBox ID="txtValue" runat="server" Text='<%#Eval("Value")%>' />
                            <asp:RequiredFieldValidator ID="Validator1" ErrorMessage="*" Display="Dynamic" ControlToValidate="txtValue" runat="server" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

And the code behind:

protected void rgUsers_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ContactId", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName", "LastName");
    }
  
    (sender as RadGrid).DataSource = table;
}
protected void rgUsers_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ContactId", typeof(int));
    table.Columns.Add("Value", typeof(int));
    for (int i = 0; i < 2; i++)
    {
        table.Rows.Add(i, i);
    }
  
    e.DetailTableView.DataSource = table;
}
  
protected void rgUsers_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.OwnerTableView.Name == "FieldLevelChanges")
    {
        string validationGroup = e.Item.OwnerTableView.ClientID;
        if (e.Item is GridDataItem)
        {
            TextBox txtValue = (TextBox)e.Item.FindControl("txtValue");
            inputManager.GetSettingByBehaviorID("txt").TargetControls.Add(new TargetInput(txtValue.UniqueID, true));
            txtValue.ValidationGroup = validationGroup;
            (e.Item.FindControl("Validator1") as RequiredFieldValidator).ValidationGroup = validationGroup;
        }
        else if (e.Item is GridCommandItem)
        {
            GridCommandItem commandItem = e.Item as GridCommandItem;
            (commandItem.FindControl("btnSubmit") as Button).ValidationGroup = validationGroup;
        }
    
}
Tags
Input
Asked by
Andy
Top achievements
Rank 2
Answers by
Angel Petrov
Telerik team
Andy
Top achievements
Rank 2
Share this question
or