This is a migrated thread and some comments may be shown as answers.

Rad Grid Insert/update

2 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 06 Apr 2012, 07:45 PM
I have the following simple grid that keeps track of a Do Not Call list.  I want to ensure that the phone number that gets put in the database is a 10 digit number with not spaces, dashs.  Example 1234567890.  Not 123 456-7890 Not (123) 456-7890.

Any help would be great.

<asp:SqlDataSource ID="sqlDNC" runat="server"
    ConnectionString="<%$ ConnectionStrings:ECC_Outbound_ConnectionString %>"
    SelectCommand="SELECT * FROM [DoNotCallList] ORDER BY [ID] DESC"
        DeleteCommand="DELETE FROM [DoNotCallList] WHERE [ID] = @ID"
        InsertCommand="INSERT INTO [DoNotCallList] ([PhoneNumber], [Date_Modified]) VALUES (@PhoneNumber,Getdate())"
        UpdateCommand="UPDATE [DoNotCallList] SET [PhoneNumber] = @PhoneNumber, [Date_Modified] = Getdate() WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="PhoneNumber" Type="String" />
            <asp:Parameter Name="Date_Modified" Type="DateTime" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="PhoneNumber" Type="String" />
            <asp:Parameter Name="Date_Modified" Type="DateTime" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0"
    DataSourceID="sqlDNC" GridLines="None" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" Width="600px" AllowAutomaticInserts="True" AllowAutomaticDeletes="true"
        AllowAutomaticUpdates="True" PageSize="25" AllowFilteringByColumn="True"
        AutoGenerateDeleteColumn="True">
<GroupingSettings CaseSensitive="false"/>
<MasterTableView DataSourceID="sqlDNC" AllowSorting="False"
            DataKeyNames="ID" CommandItemDisplay="Top">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
    <Columns>
        <telerik:GridTemplateColumn UniqueName="TemplateColumn"  AllowFiltering="False" >
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
                 <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
 
        <telerik:GridBoundColumn DataField="PhoneNumber"
            FilterControlAltText="Filter PhoneNumber column" HeaderText="PhoneNumber"
            SortExpression="PhoneNumber" UniqueName="PhoneNumber" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
            ShowFilterIcon="false" FilterControlWidth="150px">
        </telerik:GridBoundColumn>
        <telerik:GridTemplateColumn DataField="Date_Modified"
            DataType="System.DateTime" FilterControlAltText="Filter Date_Modified column"
            HeaderText="Date Modified" SortExpression="Date_Modified"
            UniqueName="Date_Modified"  AllowFiltering="False" >
            <EditItemTemplate>
                <asp:Label ID="Date_ModifiedLabel" runat="server"
                    Text='<%# Eval("Date_Modified") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Date_ModifiedLabel" runat="server"
                    Text='<%# Eval("Date_Modified") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
    </Columns>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
 
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
    </telerik:RadGrid>
</asp:Content>

2 Answers, 1 is accepted

Sort by
0
Ryan
Top achievements
Rank 1
answered on 09 Apr 2012, 02:22 PM
Any Ideas?
0
Eyup
Telerik team
answered on 11 Apr 2012, 07:41 PM
Hello Ryan,

You can easily enable typing only for numbers and disallow other signs by declaring your PhoneNumber column as a GridNumericColumn:
<telerik:GridNumericColumn UniqueName="PhoneNumber" MaxLength="10"...> </telerik:GridNumericColumn>
The MaxLength property in the code ensures that numbers larger that 10 digits cannot be entered.

In order to prevent entering a smaller number please look at this demo where EditTemplateForm and validation controls are used:
Server-side Validation

If you don't want to use edit templates, you can find the autogenerated edit form control in the code-behind and fire a client-side event:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
       {
           GridEditableItem item = (GridEditableItem)e.Item;
           RadNumericTextBox numericBox = item["PhoneNumber"].Controls[0] as RadNumericTextBox;
           numericBox.ClientEvents.OnValueChanging = "ValueChanging";
       }
   }
Then you can alert about unwanted entered value:
<script type="text/javascript">
  function ValueChanging(sender, args) {
    var value = args.get_newValue();
if (parseInt(value) < 1000000000 || parseInt(value) > 9999999999) {
  alert("*10 digits required");
  $get(sender.get_id()).focus();
 }
 }
</script>
I hope this helps.

Greetings,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Ryan
Top achievements
Rank 1
Answers by
Ryan
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or