Problem with dynamically created RadNumericTextBox and ClientEvents

2 posts, 0 answers
  1. Drammy
    Drammy avatar
    38 posts
    Member since:
    Feb 2008

    Posted 15 Aug 2009 Link to this post

    Hi all,

    I need to create a page that dynamically creates between 1 and about 30 RadNumericTextBoxes on a page.  I have no problem with this but I need to assign a onKeyPress event to the dynamically created controls.  The javascript in the onKeyEvent needs to check the value in the RadNumericTextBox is 1 digit and NOT a 1, if so tab to the next control.  If it is a 1 allow one more digit to be entered and then tab to the next control.

    I have spent a while trying to resolve this but to no joy and to be honest I am not quite sure what state my code is currently in!

    Anyway, has anyone got any ideas about how to do this or has done it before?

    Not sure what use my code is as its probably not in its previously semi-working state, but here goes:

    1 private void DrawResultsTable(DataView EntrantsResultsData) 
    2         { 
    3  
    4             if (EntrantsResultsData != null
    5             { 
    6                 int HoleCount = EntrantsResultsData.Count; 
    7                 int RowCount = Convert.ToInt16(HoleCount / 9); 
    8                 int LeftOverCells = Convert.ToInt16(HoleCount % 9); 
    9                 int MinCells = HoleCount > 9 ? 9 : HoleCount; 
    10  
    11                 TableRow ResultsRow; 
    12                 TableCell ResultsCell; 
    13                 RadNumericTextBox ResultsRadNumericText; 
    14  
    15                 int DataIndex; 
    16  
    17                 for (int i = 0; i < RowCount; i++) 
    18                 { 
    19                     ResultsRow = new TableRow(); 
    20                     ResultsRow.CssClass = EntryRowCssClass; 
    21  
    22                     for (int y = 0; y < MinCells; y++) 
    23                     { 
    24                         DataIndex = i * 9 + y; 
    25  
    26                         ResultsCell = new TableCell(); 
    27                         ResultsCell.Text = EntrantsResultsData.Table.Rows[DataIndex]["Ordinal"].ToString() + ":"
    28                         ResultsCell.CssClass = EntryLabelCellCssClass; 
    29                         ResultsCell.Attributes["align"] = "right"
    30                         ResultsRow.Cells.Add(ResultsCell); 
    31  
    32                         ResultsCell = new TableCell(); 
    33                         ResultsCell.CssClass = EntryNumericTextBoxCellCssClass; 
    34                         ResultsRadNumericText = new RadNumericTextBox(); 
    35                         ResultsRadNumericText.Type = NumericType.Number; 
    36                         ResultsRadNumericText.ID = "EntrantHoleResult" + EntrantsResultsData.Table.Rows[DataIndex]["Ordinal"].ToString(); 
    37                         ResultsRadNumericText.Text = EntrantsResultsData.Table.Rows[DataIndex]["Score"].ToString(); 
    38                         ResultsRadNumericText.ClientEvents.OnKeyPress = "SendTab(this, 'EntrantHoleResult12',event);"
    39                         ResultsRadNumericText.Width = 14; 
    40                         ResultsRadNumericText.MinValue = 1; 
    41                         ResultsRadNumericText.MaxValue = 19; 
    42                         ResultsRadNumericText.MaxLength = 2; 
    43                         ResultsRadNumericText.NumberFormat.DecimalDigits = 0; 
    44                         ResultsRadNumericText.CssClass = EntryTextBoxCssClass; 
    45                         ResultsRadNumericText.ShowSpinButtons = false
    46                         ResultsCell.Text = ResultsRadNumericText.ClientID; 
    47  
    48                         ResultsCell.Controls.Add(ResultsRadNumericText); 
    49                         ResultsRow.Cells.Add(ResultsCell); 
    50                     } 
    51  
    52                     EntrantsResultsTable.Rows.Add(ResultsRow); 
    53                 } 
    54  
    55                 ResultsRow = new TableRow(); 
    56                 ResultsCell = new TableCell(); 
    57                 ResultsCell.Text = "<br />"
    58                 ResultsCell.ColumnSpan = (MinCells * 2); 
    59                 ResultsRow.Cells.Add(ResultsCell); 
    60                 EntrantsResultsTable.Rows.Add(ResultsRow); 
    61  
    62                 ResultsRow = new TableRow(); 
    63                 ResultsCell = new TableCell(); 
    64                 ResultsCell.ColumnSpan = (MinCells * 2); 
    65                 ResultsCell.Attributes["align"] = "right"
    66  
    67                 Button btnAddEntrantsScores = new Button(); 
    68                 btnAddEntrantsScores.ID = "btnAddEntrantsScores"
    69                 btnAddEntrantsScores.Attributes["width"] = "50px"
    70                 btnAddEntrantsScores.Text = "Save"
    71                 btnAddEntrantsScores.Click += new EventHandler(this.btnAddEntrantsScores_Click); 
    72  
    73                 Button btnCancelEntrantsScores = new Button(); 
    74                 btnCancelEntrantsScores.ID = "btnCancelEntrantsScores"
    75                 btnCancelEntrantsScores.Attributes["width"] = "50px"
    76                 btnCancelEntrantsScores.Text = "Cancel"
    77                 btnCancelEntrantsScores.Click += new EventHandler(this.btnCancelEntrantsScores_Click); 
    78  
    79  
    80                 ResultsCell.Controls.Add(btnAddEntrantsScores); 
    81                 ResultsCell.Controls.Add(btnCancelEntrantsScores); 
    82  
    83                 ResultsRow.Cells.Add(ResultsCell); 
    84  
    85                 EntrantsResultsTable.Rows.Add(ResultsRow); 
    86             } 

    1 <script type="text/javascript"
    2  
    3     function SendTab(nextField, strField, evtKeyPress) { 
    4         alert("hi"); 
    5         var aKey = evtKeyPress.keyCode ? 
    6     evtKeyPress.keyCode : evtKeyPress.which ? 
    7       evtKeyPress.which : evtKeyPress.charCode; 
    8  
    9  
    10         if (aKey == 13) { 
    11             document.getElementById(nextField).focus(); 
    12         } 
    13  
    14  
    15  
    16  
    17         //Check its text   
    18         if (aKey == 49 && document.getElementById(strField).value == '') { 
    19             return; 
    20         } 
    21         else { 
    22             document.getElementById(nextField).focus(); 
    23         } 
    24  
    25  
    26     }   
    27 </script> 


    Any help appreciated, ta.
  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 19 Aug 2009 Link to this post

    Hello KevReid,

    I suggest that you check out this help topic for more information on RadInput OnKeyPress client-side
    event.
    Additionally, I prepared a sample project following your scenario.
    Give it a try and let me know if any questions arise.

    Kind regards,
    Iana
    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.
Back to Top