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

CustomValidator and InPlace editing

1 Answer 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 25 Jun 2009, 04:32 PM
I want to create a CustomValidator that will ensure that one of two different controls has a value. How do I get a reference to both controls in the ServerValidate event? My grid looks something like:

<telerik:RadGrid ID="MyGrid" runat="server" 
    OnNeedDataSource="MyGrid_NeedDataSource" 
    OnItemDataBound="MyGrid_ItemDataBound" 
    OnInsertCommand="MyGrid_InsertCommand" 
    OnUpdateCommand="MyGrid_UpdateCommand" 
    OnDeleteCommand="MyGrid_DeleteCommand" 
    > 
    <MasterTableView DataKeyNames="Key" CommandItemDisplay="Top" EditMode="InPlace"
        <Columns> 
            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" 
                <HeaderStyle Width="40px" HorizontalAlign="Center" /> 
                <ItemStyle Width="40px" HorizontalAlign="Center" /> 
            </telerik:GridEditCommandColumn> 
 
            <telerik:GridTemplateColumn UniqueName="Category" HeaderText="Category"
                <ItemStyle Width="100px" /> 
                <HeaderStyle Width="100px" /> 
                <ItemTemplate> 
                    <%#DataBinder.Eval(Container.DataItem, "Category.Name") %> 
                </ItemTemplate> 
                <EditItemTemplate> 
                    <telerik:RadComboBox ID="CategoryEdit" runat="server" 
                        Width="100%" 
                        DataSourceID="CategoryDataSource"  
                        DataTextField="Name" DataValueField="Key" 
                        SelectedValue='<%#Bind("CategoryKey") %>'
                    </telerik:RadComboBox> 
                </EditItemTemplate> 
            </telerik:GridTemplateColumn> 
 
            <telerik:GridTemplateColumn UniqueName="Contact" HeaderText="Contact"
                <ItemStyle Width="150px" /> 
                <HeaderStyle Width="150px" /> 
                <ItemTemplate> 
                    <%#DataBinder.Eval( Container.DataItem, "ContactName" )%> 
                </ItemTemplate> 
                <EditItemTemplate> 
                    <telerik:RadComboBox ID="ContactEdit" runat="server" 
                        Width="100%" 
                        DataSourceID="ContactDataSource"  
                        DataTextField="Name" DataValueField="Key" 
                        SelectedValue='<%#Bind("ContactKey") %>'
 
                    <asp:CustomValidator ID="ContactOrDescriptionValidator" runat="server"  
                        Display="Dynamic" 
                        ErrorMessage="Item must have a contact or a description." 
                        OnServerValidate="MyGrid_ContactOrDescriptionValidator_ServerValidate" 
                        > 
                    </asp:CustomValidator> 
                </EditItemTemplate> 
            </telerik:GridTemplateColumn> 
 
            <telerik:GridTemplateColumn UniqueName="Description" HeaderText="Description"
                <ItemStyle Width="150px" /> 
                <HeaderStyle Width="150px" /> 
                <ItemTemplate> 
                    <%#DataBinder.Eval( Container.DataItem, "Description" )%> 
                </ItemTemplate> 
                <EditItemTemplate> 
                    <telerik:RadTextBox ID="DescriptionEdit" runat="server"  
                        Width="100%" MaxLength="50" 
                        Text='<%#Bind("Description") %>'
                    </telerik:RadTextBox> 
                </EditItemTemplate> 
            </telerik:GridTemplateColumn> 
 
            <telerik:GridButtonColumn ButtonType="ImageButton" UniqueName="DeleteColumn"  
                CommandName="Delete" 
                <HeaderStyle Width="20px" HorizontalAlign="Center" /> 
                <ItemStyle Width="20px" HorizontalAlign="Center" /> 
            </telerik:GridButtonColumn> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid>  


Notice that my CustomValidator does not have a ControlToValidate value because I want to ensure that either the ContactEdit combo box has a value or the description has a value. Is there a way to do this or am I required to have a custom edit form?

1 Answer, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 25 Jun 2009, 06:29 PM
I found the answer to my question. First, as I mentioned previously, I did not use the ControlToValidate. Second, I consolidated the two columns into a single column with a single editor like so:

<EditItemTemplate> 
    <div class="paddingWrapper"
        <p> 
            <telerik:RadComboBox ID="ContactEdit" runat="server" 
                Width="100%" 
                DataSourceID="ContactDataSource"  
                DataTextField="Name" DataValueField="Key" 
                SelectedValue='<%#Bind("ContactKey") %>'
        </p> 
        <style="margin-left: 20px; font-weight: bold;">Or</p> 
        <p> 
            Description:<br /> 
            <telerik:RadTextBox ID="DescriptionEdit" runat="server"  
                Width="100%" MaxLength="50" 
                Text='<%#Bind("Description") %>'
            </telerik:RadTextBox> 
        </p> 
    </div> 
 
    <asp:CustomValidator ID="ContactOrDescriptionValidator" runat="server"  
        Display="Dynamic" 
        ErrorMessage="Item must have a contact or a description." 
        OnServerValidate="ContactOrDescriptionValidator_ServerValidate"                                       
        > 
    </asp:CustomValidator> 
</EditItemTemplate> 
 

The next part was the real trick: finding the controls in the edited item. My grid does not allow multiple edited items so that made it easier. In the ServerValidate event method I simply look for the zero-th item in mygrid.EditItems and on that element I call FindControl for the control I want like so:

protected void ContactOrDescriptionValidator_ServerValidate( object sender, ServerValidateEventArgs e ) 
    e.IsValid = true
 
    var editItems = this.MyGrid.EditItems; 
    if ( editItems.Count == 0 ) 
        return
 
    var editedItem = editItems[0]; 
 
    var descriptionEdit = UIUtility.FindChildControl<RadTextBox>( editedItem, "DescriptionEdit" ); 
    if ( !string.IsNullOrEmpty( descriptionEdit.Text ) ) 
        return
 
    var contactEdit = UIUtility.FindChildControl<RadComboBox>( editedItem, "ContactEdit" ); 
    if ( !string.IsNullOrEmpty( contactEdit.SelectedValue ) 
        return
 
    e.IsValid = false
    return

The "FindChildControl" function is simply a small function I wrote that does a FindControl and tries to cast result to the type passed. Granted, it'd be nice to find a way of wiring up the ClientValidationFunction but for now using AJAX and ServerValidate will suffice.

Tags
Grid
Asked by
Thomas
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Share this question
or