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

10 posts, 1 answers
  1. T. Stanley
    T. Stanley avatar
    22 posts
    Member since:
    Sep 2007

    Posted 06 Sep 2009 Link to this post

    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

  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 10 Sep 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. T. Stanley
    T. Stanley avatar
    22 posts
    Member since:
    Sep 2007

    Posted 10 Sep 2009 Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Sep 2009 Link to this post

    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.
  6. T. Stanley
    T. Stanley avatar
    22 posts
    Member since:
    Sep 2007

    Posted 12 Sep 2009 Link to this post

    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
  7. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Sep 2009 Link to this post

    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.
  8. T. Stanley
    T. Stanley avatar
    22 posts
    Member since:
    Sep 2007

    Posted 14 Sep 2009 Link to this post

    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

  9. Srinivasaragavan
    Srinivasaragavan avatar
    5 posts
    Member since:
    Oct 2012

    Posted 20 Nov 2012 Link to this post

    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.



  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Nov 2012 Link to this post

    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.
  11. Srinivasaragavan
    Srinivasaragavan avatar
    5 posts
    Member since:
    Oct 2012

    Posted 27 Nov 2012 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017