I have a htmlInput in ASP:Repeater that I want to format in a time format (ex: 13:39 ) on keypress . So far I have this code linked to a repeater:
Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rpt.ItemDataBound If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then Dim txt As HtmlInputText = DirectCast(e.Item.FindControl("txtKmRun"), HtmlInputText) If txt IsNot Nothing Then txt.Attributes.Add("onkeypress", "return kmRun('" & txt.Value & "');") End If End If End Sub
.. and this is in JavaScript:
<script> function kmRun(myValue) { String x = myValue; x = x.substring(0, 2) + ":" + x.substring(2, x.length()); alert(x); </script>
The onkeypress attribute was onkeypress using simple javascript , and it worked, but there is no return value when changing with passing the value. So, I think the error starts there.
Additional question: when javascript part works, how to return the "converted" string value back to htmlInput ? Is there any other solution for this problem that PostBack will not use?
Thanks.
=====================
This is the working code:
Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rpt.ItemDataBound If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then Dim txt As HtmlInputText = DirectCast(e.Item.FindControl("txtKmRun"), HtmlInputText) If txt IsNot Nothing Then txt.Attributes.Add("onkeypress", "return kmRun(this);") End If End If End Sub <script> function kmRun(x) { if (x.value.length > 2) { x.value = x.value.substring(0, 2) + ":" + x.value.substring(2, x.value.length); } } </script>
source share