Validating grid in Insert mode

4 posts, 1 answers
  1. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 29 Feb 2012 Link to this post

    <telerik:GridTemplateColumn HeaderText="Image Url">
            <ItemTemplate>       
            <asp:Label ID="lblImageUrl" runat="server" Text='<%# ShortenText.Shorten(Eval("imageURL"), 20) %>' />
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID="txtImageUrl" runat="server" Text='<%# Eval("imageURL") %>' /><asp:RequiredFieldValidator
                ID="ImageUrlValidator" runat="server" ErrorMessage="Image URL is required!"
                ControlToValidate="txtImageUrl" Text="*" ValidationGroup="AdvertsGroup" /><asp:RegularExpressionValidator
                ID="ImageUrlValidator1" runat="server"
                ErrorMessage="Image URL can be only image path!" Text="*"
                ControlToValidate="txtImageUrl"
                ValidationExpression="http(s?)://([\w-]+\.)+[\w-]+(/[\w- ./]*)+\.(?:gif|jpg|jpeg|png|bmp|GIF|JPEG|JPG|PNG|BMP|Gif|Jpg|Jpeg|Png|Bmp)$"
                ValidationGroup="AdvertsGroup" />
            </EditItemTemplate>
            </telerik:GridTemplateColumn>
             
            <telerik:GridTemplateColumn HeaderText="Navigate Url">
            <ItemTemplate>       
            <asp:Label ID="lblNavigateUrl" runat="server" Text='<%# ShortenText.Shorten(Eval("navigateURL"), 20) %>' />
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID="txtNavigateUrl" runat="server" Text='<%# Eval("navigateURL") %>' /><asp:RequiredFieldValidator
                    ID="NavigateUrlValidator" runat="server" ErrorMessage="Navigate URL is required!"
                    ControlToValidate="txtNavigateUrl" Text="*" ValidationGroup="AdvertsGroup" /><asp:RegularExpressionValidator
                    ID="NavigateUrlValidator1" runat="server" ErrorMessage="Navigate URL should be valid internet url!"
                    ControlToValidate="txtNavigateUrl" Text="*"
                    ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" ValidationGroup="AdvertsGroup" />
            </EditItemTemplate>
            </telerik:GridTemplateColumn>

    Basically they should not be able to insert anything but a valid image path (URL) in the txtImageUrl and valid target url in the txtNavigateUrl fields. Both fields are required. How to achieve that? Obviously the validators i have there are ignored when i click Insert link (insert mode).

    Thank you

  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 29 Feb 2012 Link to this post

    Hello,

    protected void radgrdClass_ItemDataBound(object sender, GridItemEventArgs e)
            
                if (e.Item.IsInEditMode)
                {
                    if (e.Item is GridDataInsertItem)
                    {
                        GridEditableItem editItem = (GridEditableItem)e.Item;
                        ImageButton InsertButton = (ImageButton)editItem.FindControl("PerformInsertButton");
                        InsertButton.ValidationGroup = ""; // set your validation group here
                         
                    }
                    else
                    {
                        GridEditableItem editItem = (GridEditableItem)e.Item;
                        ImageButton updateButton = (ImageButton)editItem.FindControl("UpdateButton");
                        updateButton.ValidationGroup = ""; // set your validation group here
                    }
                }
     
            }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 29 Feb 2012 Link to this post

    I tried your suggestion AS IS but the AJAX is still spinning without validating the fields.
    Do i miss something else maybe? Thanks
  5. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 29 Feb 2012 Link to this post

    This is the solution hopefully it may help to someone else
    Protected Sub RadGrid1_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
            If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                Dim insertupdate As LinkButton
                If TypeOf e.Item Is IGridInsertItem Then
                    insertupdate = CType(e.Item.FindControl("PerformInsertButton"), LinkButton)
                Else
                    insertupdate = CType(e.Item.FindControl("UpdateButton"), LinkButton)
                End If
                insertupdate.ValidationGroup = "AdvertsGroup" ' set your validation group here
            End If
        End Sub

    And of course thanks to Jayesh Goyani he pointed me in right direction. Thank you
Back to Top