【问题标题】:Make a panel visible only if textbox validations are accepted asp.net仅当接受文本框验证时才使面板可见 asp.net
【发布时间】:2015-06-09 10:24:15
【问题描述】:

我在 asp.net 中有一个面板,该面板只有在特定文本框具有经过验证和接受的输入时才可见。在所有其他情况下,它应该保持隐藏。

文本框及其验证的代码是:

<td class="auto-style4" align="left">
    ID:
    <br />
</td>
<td class="style10">
    &nbsp;&nbsp;
    <asp:TextBox ID="TextBox1" runat="server" CssClass="txtbox" Height="25px" Width="200px" >
    </asp:TextBox>
</td>
<td >
    <asp:Panel ID="Panel1" runat="server" style="display: block;"> 
        <asp:requiredfieldvalidator ID="RequiredFieldValidator1"  ForeColor="Red" ErrorMessage="Please enter valid bug ID."  Display="Dynamic" ControlToValidate="TextBox1" runat="server" SetFocusOnError="true">*   </asp:requiredfieldvalidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ForeColor="Red" ControlToValidate="TextBox1" ErrorMessage="Bug ID should be numeric." runat="server" Display="Dynamic" SetFocusOnError="true" ValidationExpression="^\d+$">*</asp:RegularExpressionValidator>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" style="display: block;">
        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" ForeColor="Red" ControlToValidate="TextBox1" ErrorMessage="Bug ID should be numeric." runat="server" Display="Dynamic" SetFocusOnError="true" ValidationExpression="^\d+$">*</asp:RegularExpressionValidator>
    </asp:Panel>
</td>

我写了一个函数来检查它是否是一个数字,因为我不确定是否可以在下面给出的代码中直接检查验证:(要显示的面板是 Panel3)

Protected Sub RBL_EnterBugID_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles RBL_EnterBugID.SelectedIndexChanged
    If RBL_EnterBugID.SelectedItem.Text = "Yes" Then
        Panel1.Visible = True
        Panel2.Visible = False
        If IsInputNumeric(TextBox1.Text) Then
            Panel3.Visible = True
        Else
            Panel3.Visible = False
        End If

    Else
        Panel1.Visible = False
        Panel2.Visible = True
        Panel3.Visible = False
        Panel5.Visible = True
    End If
End Sub

我是 asp.net 的新手,我们将不胜感激。谢谢。

【问题讨论】:

  • 您在执行此操作时是否遇到任何错误?
  • 不,我没有遇到任何错误;但是面板永远不会变得可见
  • 尝试在RBL_EnterBugID_SelectedIndexChanged 设置一个断点,看看你得到了什么结果

标签: asp.net vb.net validation


【解决方案1】:

您似乎在RBL_EnterBugID_SelectedIndexChanged 的事件代码中隐藏/显示了 PANEL3 对象

RBL_EnterBugID_SelectedIndexChanged 是什么?它是单选按钮吗? 案例正确吗?因为YESYesyes不一样

也许你可以替换 if 语句

If RBL_EnterBugID.SelectedItem.Text = "Yes" Then

使用 if 语句

If trim(lcase(RBL_EnterBugID.SelectedItem.Text)) = "yes" Then

你也可以取出这段代码

If IsInputNumeric(TextBox1.Text) Then
  Panel3.Visible = True
Else
  Panel3.Visible = False
End If

并将其替换为

Panel3.Visible = IsInputNumeric(TextBox1.Text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多