Validate datatype rad grid

2 posts, 0 answers
  1. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 03 Jun 2009 Link to this post

    I have a rad grid that generates columns and the edit function from a database. I have figured out how to make sure an input field is not blank in the code behind. What I want to do is be able to check to see if the textbox input is an integer within the edit columns. I don't really know anything about rad grids yet, I just pieced together things from the tutorials. How would I validate to make sure it is a number? Here is my aspx file
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="RadGrid.aspx.vb" Inherits="RadGrid" %>

    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="ScriptManager1" runat="server"
            EnableTheming="True">
        </telerik:RadScriptManager>
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True"
            AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True"
            DataSourceID="SqlDataSource1" GridLines="None">
    <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="Top" DataKeyNames="id"
                DataSourceID="SqlDataSource1">
        <Columns>
            <telerik:GridEditCommandColumn>
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn DataField="id" DataType="System.Int32" HeaderText="id"
                ReadOnly="True" SortExpression="id" UniqueName="id">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="title" HeaderText="title"
                SortExpression="title" UniqueName="title">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="description" HeaderText="description"
                SortExpression="description" UniqueName="description">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="price" DataType="System.Int32"
                HeaderText="price" SortExpression="price" UniqueName="price">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="quantity" DataType="System.Int32"
                HeaderText="quantity" SortExpression="quantity" UniqueName="quantity">
            </telerik:GridBoundColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn UniqueName="EditCommandColumn1">
            </EditColumn>
        </EditFormSettings>
    </MasterTableView>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConflictDetection="CompareAllValues"
            ConnectionString="<%$ ConnectionStrings:TestDevConnectionString %>"
            DeleteCommand="DELETE FROM [Data] WHERE [id] = @original_id AND (([title] = @original_title) OR ([title] IS NULL AND @original_title IS NULL)) AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL)) AND (([price] = @original_price) OR ([price] IS NULL AND @original_price IS NULL)) AND (([quantity] = @original_quantity) OR ([quantity] IS NULL AND @original_quantity IS NULL))"
            InsertCommand="INSERT INTO [Data] ([title], [description], [price], [quantity]) VALUES (@title, @description, @price, @quantity)"
            OldValuesParameterFormatString="original_{0}"
            SelectCommand="SELECT * FROM [Data]"
            UpdateCommand="UPDATE [Data] SET [title] = @title, [description] = @description, [price] = @price, [quantity] = @quantity WHERE [id] = @original_id AND (([title] = @original_title) OR ([title] IS NULL AND @original_title IS NULL)) AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL)) AND (([price] = @original_price) OR ([price] IS NULL AND @original_price IS NULL)) AND (([quantity] = @original_quantity) OR ([quantity] IS NULL AND @original_quantity IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_id" Type="Int32" />
                <asp:Parameter Name="original_title" Type="String" />
                <asp:Parameter Name="original_description" Type="String" />
                <asp:Parameter Name="original_price" Type="Int32" />
                <asp:Parameter Name="original_quantity" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="title" Type="String" />
                <asp:Parameter Name="description" Type="String" />
                <asp:Parameter Name="price" Type="Int32" />
                <asp:Parameter Name="quantity" Type="Int32" />
                <asp:Parameter Name="original_id" Type="Int32" />
                <asp:Parameter Name="original_title" Type="String" />
                <asp:Parameter Name="original_description" Type="String" />
                <asp:Parameter Name="original_price" Type="Int32" />
                <asp:Parameter Name="original_quantity" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="title" Type="String" />
                <asp:Parameter Name="description" Type="String" />
                <asp:Parameter Name="price" Type="Int32" />
                <asp:Parameter Name="quantity" Type="Int32" />
            </InsertParameters>
        </asp:SqlDataSource>
        <telerik:RadAjaxManager runat="server" EnableHistory="True">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        </form>
    </body>
    </html>

    Here is my vb file
    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Web.UI.HtmlControls
    Imports Telerik.Web.UI
    Partial Class RadGrid
        Inherits System.Web.UI.Page

       

        Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
            'check out the function in utilities for the textbox requirement validation
            Utilities.radValidateText(e, "title", "TitleRequired", "Title is required")
            Utilities.radValidateText(e, "description", "DescRequired", "Description is required")

        End Sub
    End Class

    And here is the function in my utilities class

        'this function takes in an object from a radgrid
        'the column name and a unique id for the validation name
        'it then makes sure that there is text within a textbox
        Public Shared Function radValidateText(ByVal e As Object, ByVal columnName As String, ByVal validatorID As String, ByVal errorMsg As String)

            
            If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
            
                Dim editor As GridTextBoxColumnEditor = DirectCast(item.EditManager.GetColumnEditor(columnName), GridTextBoxColumnEditor)
            
                Dim cell As TableCell = DirectCast(Editor.TextBoxControl.Parent, TableCell)

                Dim validator As New RequiredFieldValidator()

                editor.TextBoxControl.ID = validatorID

                validator.ControlToValidate = editor.TextBoxControl.ID
                validator.ErrorMessage = errorMsg
                cell.Controls.Add(validator)
            End If

        End Function 'radValidateText
    Basically, I need a similar function to say "this column must be an number" if they enter letters. Thanks in advance,





  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 09 Jun 2009 Link to this post

    Hi,

    You may consider using a CompareValidator and set its Operation property's value  to be DataTypeCheck and Type property to be Integer.

    Best wishes,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top