Radgrid Insert Validation

15 posts, 1 answers
  1. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 24 Apr 2014 Link to this post

    Hello 
    I have this problem:

    when i insert data into radgrid, i need to check if one textfield(<telerik:GridBoundColumn>) isnt empty, using a requiredfieldvalidator.
    but when i upload a file to the radasyncupload(<telerik:GridAttachmentColumn>) and if i leave textfield empty, the required field validator doesnt work as suposed, a postback event happens and i loose the file.

    im using radajaxmanagerproxy. i cant use radajaxmanager.


    <telerik:GridBoundColumn ...>
                        <ColumnValidationSettings EnableRequiredFieldValidation="true">
                            <RequiredFieldValidator SetFocusOnError="true" ForeColor="Red" Text="*" ToolTip="Empty Field"><span>*</span></RequiredFieldValidator>
                            <ModelErrorMessage Text=""></ModelErrorMessage>
                        </ColumnValidationSettings>
    </telerik:GridBoundColumn>


    any help would be appreciated.
    thanks in advance


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

    Posted 24 Apr 2014 in reply to MikeS Link to this post

    Hi Mike,

    I was not able to reproduce such an issue at me end. Can you try adding the RequiredFieldValidator from the server side as below. If this doesn't help, provide your full code snippet.

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

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 24 Apr 2014 Link to this post

    Hi Princy, Thanks for the help

    I wasnt able to create a server side validation, and actually, if i would do that, a postback event would be called on insert before the validation, or am i wrong?
    if so, because i use radasyncupload, i really need to not postback, or i loose the file uploaded.

    i just need the requiredfieldvalidator to work, arleady search arround and people say that it doesnt work well with radajaxmanagerproxy.
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Apr 2014 in reply to MikeS Link to this post

    Hi Mike,

    Please take a look at the sample code snippet i tried, it works fine at my end. Please provide you full code snippet if this doesn't help.

    ASPX:
    <telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" AllowPaging="true" AutoGenerateEditColumn="true" AllowSorting="true">
        <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
                    <ColumnValidationSettings EnableRequiredFieldValidation="true">
                        <RequiredFieldValidator SetFocusOnError="true" ForeColor="Red" Text="*" ToolTip="Empty Field"><span>*</span>  </RequiredFieldValidator>
                    </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity">
                </telerik:GridBoundColumn>
                <telerik:GridAttachmentColumn SortExpression="ShipName" UploadControlType="RadAsyncUpload" DataSourceID="SqlDataSource1" MaxFileSize="1048576" EditFormHeaderTextFormat="Upload File:" HeaderText="ShipName" AttachmentDataField="BinaryData" AttachmentKeyFields="EmployeeID" FileNameTextField="ShipName" DataTextField="ShipName" UniqueName="AttachmentColumn">
                </telerik:GridAttachmentColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT  * FROM [Orders]"></asp:SqlDataSource>

    Thanks,
    Princy
  6. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 28 Apr 2014 Link to this post

    Hello Princy, thanks for the reply.

    i've already tried those two aproaches, but with no sucess. This methods work in other aspx pages i have, but not in this. 

    here's some things about this problem: i'm using several radgrids with a radTabStrip. also, i use RadComboBox to select diferent companies and refresh those radgrids. the radtabstrip calls for ascx pages.

    im getting angry rigth now, it was easy to implement in other grids, but it refuses to work now.

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

    Posted 28 Apr 2014 in reply to MikeS Link to this post

    Hi Mike,

    Unfortunately I couldn't replicate the issue at my end. Please have a look into the sample code snippet which works fine at my end. Please provide your code if it doesn't help.

    ASPX:
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <telerik:RadTabStrip ID="RadTabStrip1" runat="server" MultiPageID="RadMultiPage1">
        <Tabs>
            <telerik:RadTab Text="Tab1" PageViewID="RadPageView1">
            </telerik:RadTab>
        </Tabs>
    </telerik:RadTabStrip>
    <telerik:RadMultiPage ID="RadMultiPage1" runat="server">
        <telerik:RadPageView ID="RadPageView1" runat="server">
            <uc1:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" />
        </telerik:RadPageView>
    </telerik:RadMultiPage>

    UserControl Page:
    <telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" AllowPaging="true" AutoGenerateEditColumn="true" AllowSorting="true">
        <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
                    <ColumnValidationSettings EnableRequiredFieldValidation="true">
                        <RequiredFieldValidator SetFocusOnError="true" ForeColor="Red" Text="*" ToolTip="Empty Field"><span>*</span>  </RequiredFieldValidator>
                    </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity">
                </telerik:GridBoundColumn>
                <telerik:GridAttachmentColumn SortExpression="ShipName" UploadControlType="RadAsyncUpload" DataSourceID="SqlDataSource1" MaxFileSize="1048576" EditFormHeaderTextFormat="Upload File:" HeaderText="ShipName" AttachmentDataField="BinaryData" AttachmentKeyFields="EmployeeID" FileNameTextField="ShipName" DataTextField="ShipName" UniqueName="AttachmentColumn">
                </telerik:GridAttachmentColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    Thanks,
    Princy.
  8. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 28 Apr 2014 Link to this post

    Hello,

    this is my code:

    This is my default page:
     
    <telerik:RadAjaxLoadingPanel ID="radajaxpanel1" runat="server" Skin="Metro" MinDisplayTime="10"></telerik:RadAjaxLoadingPanel>
    <telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />
     
    <telerik:RadAjaxPanel ID="AjaxPanel1" runat="server" LoadingPanelID="radajaxpanel1">
        <div>
            <div id="Link" style="position: fixed; z-index: 4000; top: 0px; display: inline-block; width: 98%;" class="pull-left">
                <telerik:RadComboBox
                    ID="selcomp"
                    runat="server"
                    AllowCustomText="false"
                    MarkFirstMatch="true"
                    Width="100%"
                    EnableViewState="true"
                    EmptyMessage="Select Company"
                    AutoPostBack="true"
                    CausesValidation="false"
                    Font-Size="14px" Font-Bold="True">
                </telerik:RadComboBox>
            </div>
        </div>
        <div id="formPanelbar" class="formPanelbar">
            <telerik:RadTabStrip runat="server" ID="RadTabStrip1" MultiPageID="RadMultiPage1" SelectedIndex="0" Skin="Web20" OnClientTabSelected="OnClientTabSelected">
                <Tabs>
                    <telerik:RadTab Text="InitPage" Selected="True"></telerik:RadTab>
                    <telerik:RadTab Text="Page2"></telerik:RadTab>
                </Tabs>
            </telerik:RadTabStrip>
            <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="0">
                <telerik:RadPageView ID="RadPageView1" runat="server">
                    .
            . PAGE CODE HERE
            .
     
                </telerik:RadPageView>
                <telerik:RadPageView ID="RadPageView2" runat="server">
                    <uc1:Page2 runat="server" ID="Page2" />
                </telerik:RadPageView>
    </telerik:RadAjaxPanel>
     
     
     
    This is my Page2(resumed)
     
    <telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
     
     
     
        <telerik:RadGrid
                ID="RadGrid1"
                Width="100%"
                CssClass="MyGrid"
                runat="server"
                AllowAutomaticDeletes="True"
                AllowAutomaticInserts="True"
                AllowAutomaticUpdates="True"
                AutoGenerateColumns="False"
                DataSourceID="DataSource1"
                AllowFilteringByColumn="True"
                AllowSorting="True"
                AllowPaging ="true"
                CellSpacing="0"
                GridLines="None"
                GroupingSettings-CaseSensitive="false">
            <PagerStyle AlwaysVisible="True" Mode="Slider"></PagerStyle>
        
            <MasterTableView Width="100%"
                CommandItemDisplay="Top"
                EditMode="InPlace"
                DataKeyNames="ID"
                DataSourceID="DataSource1"
                HorizontalAlign="NotSet"
                AutoGenerateColumns="False"
                InsertItemDisplay="Bottom">
                <CommandItemSettings
                    ShowAddNewRecordButton="false"
                    ShowExportToExcelButton="True"
                    ShowExportToPdfButton="false"
                    ShowExportToWordButton="false"
                />
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <HeaderStyle Width="70px"></HeaderStyle>
                        <ItemStyle CssClass="MyImageButton" Width="70px"></ItemStyle>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn ConfirmText="Are you sure'?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                        UniqueName="DeleteColumn" Visible="false">
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
                    </telerik:GridButtonColumn>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" Visible="false" SortExpression="ID" UniqueName="ID" DataType="System.Int64" FilterControlAltText="Filter ID column" ReadOnly="True">
                        <ColumnValidationSettings>
                            <ModelErrorMessage Text=""></ModelErrorMessage>
                        </ColumnValidationSettings>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="IDCompany" HeaderText="IDCompany" Visible="false" SortExpression="IDCompany" UniqueName="IDCompany" DataType="System.Int64" FilterControlAltText="Filter IDCompany column" ReadOnly="True">
                        <ColumnValidationSettings>
                        <ModelErrorMessage Text=""></ModelErrorMessage>
                        </ColumnValidationSettings>
                    </telerik:GridBoundColumn>              
                    <telerik:GridBoundColumn DataField="Desc"HeaderText="Desc" SortExpression="Desc" UniqueName="Desc" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" FilterControlWidth="80%">
                        <ColumnValidationSettings EnableRequiredFieldValidation="true" RequiredFieldValidator-ValidationGroup="ValidationGroup1">
                            <RequiredFieldValidator ForeColor="Red" Text="*" Display="Dynamic" ToolTip="Must have Desc"></RequiredFieldValidator>
                            <ModelErrorMessage Text=""></ModelErrorMessage>
                        </ColumnValidationSettings>
                    </telerik:GridBoundColumn>
                    <telerik:GridAttachmentColumn SortExpression="File" UploadControlType="RadAsyncUpload" DataSourceID="sdsFile" MaxFileSize="1048576"
                        EditFormHeaderTextFormat="File:" HeaderText="File" AttachmentDataField="File"
                        AttachmentKeyFields="ID" FileNameTextField="FileName" DataTextField="FileName"
                        UniqueName="AttchmentColumn" FilterControlWidth="80%">
                    </telerik:GridAttachmentColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>
                </EditFormSettings>
                <PagerStyle AlwaysVisible="True" Mode="Slider" PageSizes="10;20;50;100"></PagerStyle>
            </MasterTableView>
        </telerik:RadGrid>

    thanks in advance
  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Apr 2014 in reply to MikeS Link to this post

    Hi MikeS,

    Please do the following modification in your code snippet which works fine at my end.

    ASPX:
    ...
    <
    telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" 
    UniqueName="EmployeeID" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" FilterControlWidth="80%">
        <ColumnValidationSettings EnableRequiredFieldValidation="true">
            <RequiredFieldValidator ForeColor="Red" Text="*" Display="Dynamic"  ToolTip="Must                                      have Desc">
                      <
    span>*</span
              </
    RequiredFieldValidator>
        </ColumnValidationSettings>
    </telerik:GridBoundColumn>
    ...

    Thanks,
    Princy.
  10. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 29 Apr 2014 Link to this post

    Hello Princy.

    Your solution didnt solve my problem. when i click on insert button the validation dont fire and a post back occurs.

    Im trying other approach. there is a way to know if a file was selected to radasyncupload, client side? this way i could verify before insert, and if radasyncupload was empty, i could cancel postback and make an alert. What do you think?

    thanks for all the help.
  11. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 29 Apr 2014 Link to this post

    that would not solve my textbox validation problem, but i also need to validate radasyncupload field. 
  12. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 30 Apr 2014 in reply to MikeS Link to this post

    Hi MikeS,

    In order to validate the RadAsyncUpload please try the following code snippet which works fine t my end. 

    ASPX:
    ...
    <
    telerik:GridAttachmentColumn SortExpression="ShipName" UploadControlType="RadAsyncUpload"
        DataSourceID="SqlDataSource1" MaxFileSize="1048576" EditFormHeaderTextFormat="Upload File:"
        HeaderText="ShipName" AttachmentDataField="BinaryData" AttachmentKeyFields="EmployeeID"
        FileNameTextField="ShipName" DataTextField="ShipName" UniqueName="AttachmentColumn">
    </telerik:GridAttachmentColumn>
    <telerik:GridTemplateColumn>
        <EditItemTemplate>
            <asp:Label ID="Label1" runat="server">
            </asp:Label>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>
    ...

    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName | e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            RadAsyncUpload upload = (RadAsyncUpload)item["AttachmentColumn"].Controls[0];
            if (upload.UploadedFiles.Count == 0)
            {
                e.Canceled = true;
                Label label = (Label)item.FindControl("Label1");
                label.Style.Add("margin-left", "-165%");
                label.ForeColor = System.Drawing.Color.Red ;
                label.Text = "*";
            }
        }
    }

    Thanks,
    Princy.
  13. MikeS
    MikeS avatar
    32 posts
    Member since:
    Mar 2014

    Posted 30 Apr 2014 Link to this post

    Hello Princy,

    your snippet helped me solve the problem with the radasync and i just solved the problem with the textbox validation too(using js).
    thanks for everything!
  14. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 06 Jan 2015 in reply to MikeS Link to this post

    I have a similar problem where the RadGrid columns have "ColumnValidationSettings", but the page still posts-back to a ItemCommand which then causes an error (due to missing data).
  15. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 06 Jan 2015 in reply to MikeS Link to this post

    Why would the page be posting-back if ColumnValidationSettings are set?
  16. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 09 Jan 2015 Link to this post

    Hello,

    Some of the Button controls rendered in RadGrid have ValidationGroup set. Because of this client-side validation is not performed, validation is performed on the server instead.

    With this said, would you elaborate in more detail what is your exact scenario? I would appreciate it if you could share your code or send in in a support ticket. This will enable us to better understand your setup and assist you in achieving the behavior you are looking for.

    Regards,
    Viktor Tachev
    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.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017