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

Problem with dynamically created RadNumericTextBox and ClientEvents

1 Answer 103 Views
Input
This is a migrated thread and some comments may be shown as answers.
Drammy
Top achievements
Rank 1
Drammy asked on 15 Aug 2009, 07:31 PM
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:

private void DrawResultsTable(DataView EntrantsResultsData) 
        { 
 
            if (EntrantsResultsData != null
            { 
                int HoleCount = EntrantsResultsData.Count; 
                int RowCount = Convert.ToInt16(HoleCount / 9); 
                int LeftOverCells = Convert.ToInt16(HoleCount % 9); 
                int MinCells = HoleCount > 9 ? 9 : HoleCount; 
 
                TableRow ResultsRow; 
                TableCell ResultsCell; 
                RadNumericTextBox ResultsRadNumericText; 
 
                int DataIndex; 
 
                for (int i = 0; i < RowCount; i++) 
                { 
                    ResultsRow = new TableRow(); 
                    ResultsRow.CssClass = EntryRowCssClass; 
 
                    for (int y = 0; y < MinCells; y++) 
                    { 
                        DataIndex = i * 9 + y; 
 
                        ResultsCell = new TableCell(); 
                        ResultsCell.Text = EntrantsResultsData.Table.Rows[DataIndex]["Ordinal"].ToString() + ":"
                        ResultsCell.CssClass = EntryLabelCellCssClass; 
                        ResultsCell.Attributes["align"] = "right"
                        ResultsRow.Cells.Add(ResultsCell); 
 
                        ResultsCell = new TableCell(); 
                        ResultsCell.CssClass = EntryNumericTextBoxCellCssClass; 
                        ResultsRadNumericText = new RadNumericTextBox(); 
                        ResultsRadNumericText.Type = NumericType.Number; 
                        ResultsRadNumericText.ID = "EntrantHoleResult" + EntrantsResultsData.Table.Rows[DataIndex]["Ordinal"].ToString(); 
                        ResultsRadNumericText.Text = EntrantsResultsData.Table.Rows[DataIndex]["Score"].ToString(); 
                        ResultsRadNumericText.ClientEvents.OnKeyPress = "SendTab(this, 'EntrantHoleResult12',event);"
                        ResultsRadNumericText.Width = 14; 
                        ResultsRadNumericText.MinValue = 1; 
                        ResultsRadNumericText.MaxValue = 19; 
                        ResultsRadNumericText.MaxLength = 2; 
                        ResultsRadNumericText.NumberFormat.DecimalDigits = 0; 
                        ResultsRadNumericText.CssClass = EntryTextBoxCssClass; 
                        ResultsRadNumericText.ShowSpinButtons = false
                        ResultsCell.Text = ResultsRadNumericText.ClientID; 
 
                        ResultsCell.Controls.Add(ResultsRadNumericText); 
                        ResultsRow.Cells.Add(ResultsCell); 
                    } 
 
                    EntrantsResultsTable.Rows.Add(ResultsRow); 
                } 
 
                ResultsRow = new TableRow(); 
                ResultsCell = new TableCell(); 
                ResultsCell.Text = "<br />"
                ResultsCell.ColumnSpan = (MinCells * 2); 
                ResultsRow.Cells.Add(ResultsCell); 
                EntrantsResultsTable.Rows.Add(ResultsRow); 
 
                ResultsRow = new TableRow(); 
                ResultsCell = new TableCell(); 
                ResultsCell.ColumnSpan = (MinCells * 2); 
                ResultsCell.Attributes["align"] = "right"
 
                Button btnAddEntrantsScores = new Button(); 
                btnAddEntrantsScores.ID = "btnAddEntrantsScores"
                btnAddEntrantsScores.Attributes["width"] = "50px"
                btnAddEntrantsScores.Text = "Save"
                btnAddEntrantsScores.Click += new EventHandler(this.btnAddEntrantsScores_Click); 
 
                Button btnCancelEntrantsScores = new Button(); 
                btnCancelEntrantsScores.ID = "btnCancelEntrantsScores"
                btnCancelEntrantsScores.Attributes["width"] = "50px"
                btnCancelEntrantsScores.Text = "Cancel"
                btnCancelEntrantsScores.Click += new EventHandler(this.btnCancelEntrantsScores_Click); 
 
 
                ResultsCell.Controls.Add(btnAddEntrantsScores); 
                ResultsCell.Controls.Add(btnCancelEntrantsScores); 
 
                ResultsRow.Cells.Add(ResultsCell); 
 
                EntrantsResultsTable.Rows.Add(ResultsRow); 
            } 

<script type="text/javascript"
 
    function SendTab(nextField, strField, evtKeyPress) { 
        alert("hi"); 
        var aKey = evtKeyPress.keyCode ? 
    evtKeyPress.keyCode : evtKeyPress.which ? 
      evtKeyPress.which : evtKeyPress.charCode; 
 
 
        if (aKey == 13) { 
            document.getElementById(nextField).focus(); 
        } 
 
 
 
 
        //Check its text   
        if (aKey == 49 && document.getElementById(strField).value == '') { 
            return; 
        } 
        else { 
            document.getElementById(nextField).focus(); 
        } 
 
 
    }   
</script> 


Any help appreciated, ta.

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 19 Aug 2009, 11:18 AM
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.
Tags
Input
Asked by
Drammy
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or