我在ASP:Repeater
的中有此htmlInput
,我希望在其keypress
上以时间格式(例如:13:39
)对其进行格式化。到目前为止,我有这样的代码在转发数据绑定:使用javascript在HTMLInput中为字符串添加字符
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
..和这是在JavaScript:
<script>
function kmRun(myValue) {
String x = myValue;
x = x.substring(0, 2) + ":" + x.substring(2, x.length());
alert(x); //alert to test display but is not working
//HOW TO PASS x VALUE TO BACK TO THE TEXTBOX?
}
</script>
测试onkeypress
属性用一个简单的javascript警报消息和它的工作,但是当与修改价值传递,没有回报价值。所以我猜,错误从那里开始。
其他问题是当javascript部分工作时,如何将“已转换”字符串值返回到htmlInput
?有没有其他解决方案可以解决这个问题,不会使用PostBack
?
谢谢。
===================
这是工作代码:
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>
谢谢你,先生!现在正在工作。 – eirishainjel
不客气@eirishainjel。如果您需要更多帮助,您可以随时问。 :) –
是的。再次感谢! – eirishainjel