Required Field Validator in RadGrid

9 posts, 0 answers
  1. Corey
    Corey avatar
    14 posts
    Member since:
    Apr 2011

    Posted 02 Sep 2011 Link to this post

    I have a radgrid using inplace editting inside of an ajaxpanel.

    I need to have a requiredfieldvalidator to prevent a field entry in the radgrid.

    Can this be done?  I've removed the ajaxpanel and had the same issue.
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 03 Sep 2011 Link to this post

    Hello COREY,

    Grid / Flexible Server-Side Validation

    or

    <rad:RadGrid id="RadGrid1" runat="server" AutoGenerateColumns="False">
    <MasterTableView>
       <Columns>
       <rad:GridBoundColumn UniqueName="ContactName" HeaderText="ContactName" DataField="ContactName"></rad:GridBoundColumn>
       <rad:GridEditCommandColumn UniqueName="EditCommandColumn"></rad:GridEditCommandColumn>
       </Columns>
    </MasterTableView>
    </rad:RadGrid>


    private void RadGrid1_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e)
    {
      if(e.Item is GridEditableItem && e.Item.IsInEditMode)
      {
         GridEditableItem item = e.Item as GridEditableItem;
         GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor( "ContactName");
         TableCell cell = (TableCell)editor.TextBoxControl.Parent;
      
         RequiredFieldValidator validator = new RequiredFieldValidator();
         editor.TextBoxControl.ID = "ID_for_validation";
         validator.ControlToValidate = editor.TextBoxControl.ID;
         validator.ErrorMessage = "*";
         cell.Controls.Add(validator);
      }
    }


    Let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Twan
    Twan avatar
    3 posts
    Member since:
    Dec 2012

    Posted 18 Jul 2013 Link to this post

    This described solution is broken when upgrading from 2012.4.411.40 to 2013.2.717.40.
    A follow up call (OnDataBind) will throw Object reference not set to instance of an object.
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2013 Link to this post

    Hi,

    The above code snippet is for versions before Q1 2013.So please try the below code snippet.

    ASPX:
    <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />

     C#:
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
               GridEditableItem item = e.Item as GridEditableItem;
               GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("ShipCity");
               TableCell cell = (TableCell)editor.TextBoxControl.Parent;
               RequiredFieldValidator validator = new RequiredFieldValidator();
               validator.ControlToValidate = editor.TextBoxControl.ID;
               validator.ErrorMessage = "*";
               cell.Controls.Add(validator);
           }

    You can also have a look at this documentation on Validation.

    Thanks,
    Shinu
  6. Twan
    Twan avatar
    3 posts
    Member since:
    Dec 2012

    Posted 18 Jul 2013 Link to this post

    That C# code is what is causing the bug not the .aspx
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2013 Link to this post

    Hi Twan

    The C# code was working fine at my end,have you tried the same code.The version im using is 2013.2.611.40.Please try the code in the documentation for the latest version.There are two codes for versions before Q1 2013 and after.So make sure you have used the right code.
    If this doesn't help,please provide your full code.

    Thanks,
    Shinu
  8. Twan
    Twan avatar
    3 posts
    Member since:
    Dec 2012

    Posted 25 Apr 2014 in reply to Shinu Link to this post

    Hey,

    I have found the problem in our code. In a previous implementation it was recommended to set the id of the column editor in order for the required field validator to work. It was a line like this:

    editor.TextBoxControl.ID = "someId";

    The same goes for DropDown editor. Removing this line fixed the issue.

    Regards,

    Twan
  9. Mohd
    Mohd avatar
    3 posts
    Member since:
    Oct 2015

    Posted 07 Oct 2015 Link to this post

    <telerik:GridTemplateColumn HeaderText="Completion Date" UniqueName="CompletionDate"
                                            DataType="System.DateTime" SortExpression="CompletionDate">
                                            <ItemTemplate>
                                                <asp:Label runat="server" ID="CompletionDateLabel"  DataFormatString="{0:d}"  Text='<%# Eval("CompletionDate") %>'>
                                                </asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <telerik:RadDatePicker ID="CompletionDatePicker" runat="server" DbSelectedDate='<%# Bind("CompletionDate") %>'>
                                                </telerik:RadDatePicker>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                                                    ControlToValidate="CompletionDatePicker"></asp:RequiredFieldValidator>
                                            </EditItemTemplate>
                                        </telerik:GridTemplateColumn>


    This works for me
  10. Mohd
    Mohd avatar
    3 posts
    Member since:
    Oct 2015

    Posted 07 Oct 2015 in reply to Twan Link to this post

    <telerik:GridTemplateColumn HeaderText="Completion Date" UniqueName="CompletionDate"
                                            DataType="System.DateTime" SortExpression="CompletionDate">
                                            <ItemTemplate>
                                                <asp:Label runat="server" ID="CompletionDateLabel"  DataFormatString="{0:d}"  Text='<%# Eval("CompletionDate") %>'>
                                                </asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <telerik:RadDatePicker ID="CompletionDatePicker" runat="server" DbSelectedDate='<%# Bind("CompletionDate") %>'>
                                                </telerik:RadDatePicker>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                                                    ControlToValidate="CompletionDatePicker"></asp:RequiredFieldValidator>
                                            </EditItemTemplate>
                                        </telerik:GridTemplateColumn>
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017