仅接受数字用于文本框

时间:2018-09-23 19:37:50

标签: asp.net vb.net

正在研究VB的ASP.NET项目的学生,该项目试图限制用户使用CASE语句在文本框中输入任何可接受的数字,但我一直遇到System.EventArgs错误。

我的代码:

 Protected Sub ValueBox1_TextChanged(sender As Object, e As EventArgs) Handles ValueBox1.TextChanged
    Select Case e.KeyChar
    Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
    e.Handled = False
    Case Else
    e.Handled = True
    End Select
    End Sub

错误='KeyChar'和'Handled'不是'System.EventArgs'的成员

我尝试将其更改为KeyPress事件

 (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)Handles TextBox1.KeyPress

但是随后找不到消息更改为“ KeyPress”。我知道我已经接近了,但是尝试了许多其他更改和建议,感觉就像我在转圈。

2 个答案:

答案 0 :(得分:0)

您注意到, else{ document.getElementById(i).innerHTML = ABCD[i] + AnswerArray[rngIndex[i]]; if (rngIndex[i] == 3) { // Notice that the actual call to Correct is wrapped: document.getElementById(i).addEventListener("click", function(event){ Correct(event, this.id); }); } else{ // Notice that the actual call to Incorrect is wrapped: document.getElementById(i).addEventListener("click", function(){ Incorrect(this.id); }); } i++; } } //end of a for loop } //end of a function function Correct(event, id){ alert('good nice'); document.getElementById(id).removeEventListener("click", Correct(this.id)); drawClock(); } function Incorrect(event, id){ incorrect++ alert(incorrect + " incorrect") document.getElementById(id).removeEventListener("click", Correct(this.id)); } 类型(所有其他EventArgs的基本类型)不包含<Something>EventArgs属性(实际上根本没有属性)。
服务器端.KeyChar事件在Web应用程序中无用。
因此,您的选择是在客户端(在浏览器中)检查输入,并在服务器端进行额外的验证。

  1. KeyPressed
    呈现<asp:TextBox ID="txtNum" runat="server" TextMode="Number" min="0" max="20" />
    允许数字,<input name="txtNum" type="number" id="txtNum" min="0" max="20">

  2. +, -, .
    验证更改后的 输入,不允许在错误修复之前提交表单。

  3. 使用javascript检查客户端的每个按键。

    <asp:TextBox runat="server" TextMode="SingleLine" ID="txtNum2" />
    <asp:RangeValidator runat="server" ID="valNum2" ControlToValidate="txtNum2" Display="Dynamic" 
        ErrorMessage="Numbers only" MaximumValue="20" MinimumValue="0" Type="Integer"></asp:RangeValidator>

另外在服务器端检查/验证输入。

<asp:TextBox runat="server" ID="txtNum3" OnTextChanged="txtNum3_TextChanged" onkeydown="return checkKey(event)" />
<script>
    function checkKey(evt) {
        if ('0123456789'.indexOf(evt.key) == -1)
            return false;
        return true;
    }
</script>

答案 1 :(得分:-1)

在经典模式“这是我之前准备的那个”中...

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If Not (e.KeyChar >= "0" And e.KeyChar <= "9") Then
        e.KeyChar = Chr(0)
    End If
End Sub

在您的帖子中,您将ValueBox1换成TextBox1KeyChar对通用EventArgs无效,但对特定的KeyPressEventArgs有效。

如果这在您的上下文中不起作用,请检查您的解决方案引用,因为该代码过去对我有用。

相关问题