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

Limit length of text in Edit Mode

1 Answer 35 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 24 Jan 2010, 06:32 PM
Hi All

I have a RadGrid that uses the standard edit mode form, and I am try to complete
the validation as in the declaration shown below:
<telerik:GridTemplateColumn DataField="FirstName"   
            HeaderText="FirstName" SortExpression="FirstName"   
            UniqueName="FirstName">  
            <EditItemTemplate> 
                <asp:TextBox ID="FirstNameTextBox" runat="server"   
                    Text='<%# Bind("FirstName") %>' MaxLength="25">  
                    </asp:TextBox> 
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1"   
                    runat="server"   
                    ControlToValidate="FirstNameTextBox"   
                    Display="Dynamic"   
                    ErrorMessage="First name required"   
                    SetFocusOnError="True"></asp:RequiredFieldValidator> 
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1"   
                    runat="server" Display="Dynamic"   
                    ErrorMessage="Illegal characters"   
                    SetFocusOnError="True"   
                    ValidationExpression="^[a-zA-Z0-9 \-]+$"  ControlToValidate="FirstNameTextBox"></asp:RegularExpressionValidator> 
            </EditItemTemplate> 
The problem is that when the MaxLength property is set it prevents any chacters being entered in Edit mode. Insert Mode is OK

So I took that property out and used code behind like this:
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs)  
        If TypeOf e.Item Is GridDataItem Then  
            Dim dataItem As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim txtbx As TextBox = DirectCast(dataItem.FindControl("FirstNameTextBox"), TextBox)  
            Dim strtxt As String = txtbx.Text  
            If strtxt.Length > 25 Then  
                txtbx.Text = strtxt.Remove(25)  
            End If  
        End If  
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then  
            Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
            Dim txtbx1 As TextBox = DirectCast(editItem.FindControl("FirstNameTextBox"), TextBox)  
            Dim strtxt1 As String = txtbx1.Text  
            If strtxt1.Length > 25 Then  
                txtbx1.Text = strtxt1.Remove(25)  
            End If  
        End If  
    End Sub 

But that did not have any effect at all.

What I am doing worng here?

Thanks

Clive
PS using Q2 2009 version

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 25 Jan 2010, 11:46 AM
Hi,

I 'm a bit confused with your question.As far as I know the MaxLength property prevents a user from entering more than the specified length.This is working at my end using your exact scenario. The code you have used tries to truncate the bound data for the datafield when the grid is in edit mode,if  the text length exceeds the specified limit.
Which of these are you trying to achieve?

Thanks,
Princy
Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or