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

Finding a control in a GridTemplateColumn EditItemTemplate during a CustomValidator ServerValidate event

11 Answers 618 Views
Grid
This is a migrated thread and some comments may be shown as answers.
T. Stanley
Top achievements
Rank 1
T. Stanley asked on 07 Sep 2009, 12:30 AM
Hi.

I have a difficult problem attemting to validate a change in a RadComboBox that is in the EditItemTemplate of a RadGrid GridTemplateColumn.  I have two such combo boxes in the EditTemplate(s), and I wnat to validate the changed to value of one against the vlaue of the other.  So when the user changes the combobox "startime", the validation code examines the value (not the text) of the "endtime" combobox (and the same thing validating the "endtime" change against the "starttime").  What code must I put in the ServerValidate event for one combobox to find the control that is the other combobox?  I looked through all the local objects of the radgrid during debug, and cannot find these.  Declarative syntax below:

    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="sdsCRNAShifts" GridLines="None" Skin="Simple" 
        AutoGenerateColumns="False" AllowAutomaticUpdates="True" AllowAutomaticInserts="True" PageSize="128" Width="100%" AllowSorting="True" OnEditCommand="RadGrid1_EditCommand" > 
        <MasterTableView DataSourceID="sdsCRNAShifts" CommandItemDisplay="Top" DataKeyNames="shiftID" > 
            <Columns> 
                <telerik:GridCheckBoxColumn DataField="active" HeaderText="active" UniqueName="active" AllowFiltering="False" AllowSorting="False">  
                    <HeaderStyle Width="20px" /> 
                </telerik:GridCheckBoxColumn> 
                <telerik:GridDropDownColumn DataField="shiftType" DataSourceID="XmlDataSource1"  HeaderText="Type" 
                    ListValueField="ID" ListTextField="Text" UniqueName="shiftType" > 
                    <ItemStyle Width="60px" /> 
                    <HeaderStyle Width="60px" /> 
                </telerik:GridDropDownColumn> 
                <telerik:GridBoundColumn DataField="shiftName" HeaderText="Name" UniqueName="shiftName" ColumnEditorID="GridTextBoxColumnEditor1" > 
                    <HeaderStyle Width="120px" /> 
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="shiftDescr" HeaderText="Description" UniqueName="shiftDescr" ColumnEditorID="GridTextBoxColumnEditor2" AllowSorting="False" > 
                    <HeaderStyle Width="200px" /> 
                </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn DataField="bOff" HeaderText="BeginTime" UniqueName="bOff">  
                    <EditItemTemplate> 
                        <telerik:RadComboBox ID="bOffRadComboBox" runat="server" DataSourceID="sdsTimeText" 
                            DataTextField="timeText" DataValueField="numMins" SelectedValue='<%# Bind("bOff") %>' AutoPostBack="True" OnSelectedIndexChanged="bOffRadComboBox_SelectedIndexChanged">  
                        </telerik:RadComboBox> 
                        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="StartTime must precede EndTime" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> 
                    </EditItemTemplate> 
                    <ItemTemplate> 
                        <asp:Label ID="bOffLabel" runat="server" Text='<%# Eval("beginTime") %>'></asp:Label> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn DataField="eOff" HeaderText="EndTime" UniqueName="eOff">  
                    <EditItemTemplate> 
                        <telerik:RadComboBox ID="eOffRadComboBox" runat="server" DataSourceID="sdsTimeText" 
                            DataTextField="timeText" DataValueField="numMins" SelectedValue='<%# Bind("eOff") %>' AutoPostBack="True">  
                        </telerik:RadComboBox> 
                        &nbsp;  
                        <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="EndTime must follow StartTime" OnServerValidate="CustomValidator2_ServerValidate"></asp:CustomValidator> 
                    </EditItemTemplate> 
                    <ItemTemplate> 
                        <asp:Label ID="eOffLabel" runat="server" Text='<%# Eval("endTime") %>'></asp:Label> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridEditCommandColumn ButtonType="ImageButton">  
                    <HeaderStyle Width="50px" /> 
                </telerik:GridEditCommandColumn> 
            </Columns> 
            <EditFormSettings> 
                <EditColumn ButtonType="ImageButton" InsertText="Insert Order" UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">  
                </EditColumn> 
            </EditFormSettings> 
       </MasterTableView> 
        <ClientSettings> 
            <Selecting AllowRowSelect="True" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
 

Thank you for any and all help!

Tom Stanley

11 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 10 Sep 2009, 06:46 AM

Hi Thomas,

You can obtain a reference to the combo in the other template column (where there is no validator) as follows:

RadComboBox bOffCombo = (((GridDataItem)((CustomValidator)source).NamingContainer)).EditFormItem["bOff"].FindControl("bOffRadComboBox"); 

I hope this hleps.

Regards,

Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
T. Stanley
Top achievements
Rank 1
answered on 11 Sep 2009, 01:44 AM
Thanks so much for the reply.  Unfortunately, when I include the code you suggested:
 
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)  
    {  
        RadComboBox bOffCombo = (((GridDataItem)((CustomValidator)source).NamingContainer)).EditFormItem["bOff"].FindControl("bOffRadComboBox");  
    }  
 

I get the compiler error:

Cannot implicitly convert type 'System.Web.UI.Control' to 'Telerik.Web.UI.RadComboBox'. An explicit conversion exists (are you missing a cast?)

I am a bit confused by all the casts in this statement, so I would very much appreciate your advice on proceeding.

Thank you!

Tom Stanley
0
Princy
Top achievements
Rank 2
answered on 11 Sep 2009, 03:57 AM
Hello Tom Stanley,

Try modifying the code as shown below, to get rid of the error taht you get:
c#:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)   
    {   
        RadComboBox bOffCombo = (RadComboBox)(((GridDataItem)((CustomValidator)source).NamingContainer)).EditFormItem["bOff"].FindControl("bOffRadComboBox");   
    }   

Thanks
Princy.
0
T. Stanley
Top achievements
Rank 1
answered on 12 Sep 2009, 09:51 PM
Thank you so much  for the continued response to my problem.  I tried the code you suggested:

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)  
    {  
        RadComboBox bOffCombo = (RadComboBox)(((GridDataItem)((CustomValidator)source).NamingContainer)).EditFormItem["bOff"].FindControl("bOffRadComboBox");  
    } 

but unfortunately got a different error when this line is executed, "Unable to cast object of type 'Telerik.Web.UI.GridEditFormItem' to type 'Telerik.Web.UI.GridDataItem'."

I am sorry to be so unfamiliar with the structure of these types, but I am still lost as to how to get to the control that I need.  Thanks for any additional help you can give.

Tom Stanley
0
Accepted
Princy
Top achievements
Rank 2
answered on 14 Sep 2009, 04:28 AM
Hi Tom,

You have to type cast the EditFormItem as GridEditFormItem as shown below. This should help you get rid of cast exception:
c#:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)    
    {    
        RadComboBox bOffCombo = (RadComboBox)(((GridEditFormItem)((CustomValidator)source).NamingContainer)).EditFormItem["bOff"].FindControl("bOffRadComboBox");    
    }   

You can also refer to the following documents to understand better how to access items in RadGrid:
Accessing cells and rows
Edit forms

Thanks
Princy.
0
T. Stanley
Top achievements
Rank 1
answered on 15 Sep 2009, 12:53 AM
Thank you so much for all your help.  It was actually the following code (minus the .EditFormItem["bOff"] that worked:

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)  
    {  
        RadComboBox bOffCombo = (RadComboBox)(((GridEditFormItem)((CustomValidator)source).NamingContainer)).FindControl("bOffRadComboBox");  
    }  
 

I really appreciate your patience in helping me understand this.

Tom Stanley

0
Srinivasaragavan
Top achievements
Rank 1
answered on 20 Nov 2012, 01:20 PM
My case is that i need to access the  RadComboBox from the control that is outside the Radgrid. 
could you please help me in doing that.


I mean
I have a Button placed outside the Grid. On click of that button i need to access the radComboBox.



0
Princy
Top achievements
Rank 2
answered on 21 Nov 2012, 03:58 AM
Hi,

Please try the following code snippet to access the RadComboBox in EditItemTemplate on an external button click.

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.EditItems)
    {
        GridEditableItem itemToEdit =(GridEditableItem)item.EditFormItem;
        RadComboBox combo = (RadComboBox)itemToEdit.FindControl("RadComboBox1");
 
    }
}

Thanks,
Princy.
0
Srinivasaragavan
Top achievements
Rank 1
answered on 27 Nov 2012, 01:19 PM
Hi
this is not working for me.
So i have started using the alternate way of doing insert to the grid by using (usercontroleditform).

But i have issues in that too. So trying to find out solution for that.

Thanks
Srinivas
0
Michael
Top achievements
Rank 1
answered on 16 Feb 2018, 06:04 PM

I have a similar issue.  I have a RADGRID with 2 Columns being edited in Edit Item templates.  They are both RadDropDownLists.  I merely have one with a custom validator (the second DropDownList) and want to access the current value of the other (the first DropDownList)  in the Custom validator event code.  The CustomValidator is firing fine, I just need to get the value of the other DropDownList.   HELP!!!

How can I do this.  The code is:

<telerik:GridTemplateColumn DataField="Request_Status" Display="False" FilterControlAltText="Filter Request_Status column" HeaderText="Request Status" SortExpression="Request_Status" UniqueName="Request_Status">
    <EditItemTemplate>
<telerik:RadDropDownList runat="server" ID="RadDropDownListStatus"  DataTextField="Request_Status" SelectedValue='<%# Bind("Request_Status") %>' Width="180">
    <Items>
<telerik:DropDownListItem runat="server" Text="Requested" Value="Requested" />
<telerik:DropDownListItem runat="server" Text="Approved" Value="Approved" />
<telerik:DropDownListItem runat="server" Text="Cancelled" Value="Cancelled" />
    </Items>
    </telerik:RadDropDownList>
    </EditItemTemplate>
    <ItemTemplate>
<asp:Label ID="Request_StatusLabel" runat="server" Text='<%# Eval("Request_Status") %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="Parking_Spot_ID" DataType="System.Int32" Display="False" FilterControlAltText="Filter Parking_Spot_ID column" HeaderText="Parking Spot" SortExpression="Parking_Spot_ID" UniqueName="Parking_Spot_ID">
    <EditItemTemplate>
<telerik:RadDropDownList runat="server" ID="RadDropDownListSpotID"  DataTextField="ParkingSpotDescriptionActiveAdminNote"
DataValueField="Parking_Spot_ID" DataSourceID="SqlDataSourceAvailableSpots" SelectedValue='<%#Bind("Parking_Spot_ID") %>' Width="610">
</telerik:RadDropDownList>
<asp:CustomValidator ID="CustomValidatorParkingSpotApproved" runat="server" onservervalidate="Cus_validateSpotApproved" ErrorMessage="Error: A Parking Spot must be selected for Approved Requests" CssClass="field-validation-error" ControlToValidate="RadDropDownListSpotID"></asp:CustomValidator>
    </EditItemTemplate>
    <ItemTemplate>
<asp:Label ID="Parking_Spot_IDLabel" runat="server" Text='<%# Eval("Parking_Spot_ID") %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>
------------------------------------------------

protected void Cus_validateSpotApproved(object sender, ServerValidateEventArgs e)
        {

            if ( e.Value.ToString()=="")  // gets the text of the parking spot selected - OK
            
//TO DO - THE PROBLEM - I need the current selected value of the RadDropDownListStatus to compare here             
            
                e.IsValid = true;
            else
                e.IsValid = false;
        }

0
Eyup
Telerik team
answered on 21 Feb 2018, 08:24 AM
Hello Michael,

Once you got the parent tr row element, you can use the findControl method to access the corresponding Telerik control:
https://www.telerik.com/support/kb/aspnet-ajax/details/access-telerik-controls-on-client-side

I am sending a sample RadGrid web site to demonstrate a similar implementation. Please run the attached application and let me know if it helps you.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
T. Stanley
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
T. Stanley
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Srinivasaragavan
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or