如何获取单个错误标签以显示C#asp.net

如何获取单个错误标签以显示C#asp.net,c#,asp.net,textbox,labels,C#,Asp.net,Textbox,Labels,我在一个联系页面上工作,希望得到一些帮助,当文本框为空时,如何在个别时间显示错误标签。我使用了3个文本框。我是C#asp.net的一名新生。我真的很感谢你的时间和帮助。谢谢 //if no or incorrect entries are entered panel with red message using a label appears to remind the user of //if name, email, enquiry is blank error labels s

我在一个联系页面上工作,希望得到一些帮助,当文本框为空时,如何在个别时间显示错误标签。我使用了3个文本框。我是C#asp.net的一名新生。我真的很感谢你的时间和帮助。谢谢

 //if no or incorrect entries are entered panel with red message using a label appears to remind the user of 
    //if name, email, enquiry is blank error labels show up
    if (nameContactUsTextBox.Text == "" && emailContactUsTextBox.Text == "" && enquiryContactUsTextBox.Text == "")
    {
        nameErrorLabel.Visible = true;
        emailErrorLabel.Visible = true;
        equiryErrorLabel.Visible = true;
    }

    if (nameContactUsTextBox.Text != "" && emailContactUsTextBox.Text == "" && enquiryContactUsTextBox.Text == "")
    {

        emailErrorLabel.Visible = true;
        equiryErrorLabel.Visible = true;
    }

    if (nameContactUsTextBox.Text == "" && emailContactUsTextBox.Text != "" && enquiryContactUsTextBox.Text == "")
    {
        nameErrorLabel.Visible = true;
        equiryErrorLabel.Visible = true;
    }

    if (nameContactUsTextBox.Text == "" && emailContactUsTextBox.Text == "" && enquiryContactUsTextBox.Text != "")
    {
        nameErrorLabel.Visible = true;
        emailErrorLabel.Visible = true;

    }
aspx文件

<p>
    <table style="width: 100%;">
        <tr>
            <td class="boring">Name</td>
            <td class="auto-style1">
                <asp:TextBox ID="nameContactUsTextBox" runat="server" CssClass="boring" ToolTip="Enter Name" Width="441px"></asp:TextBox>
                <asp:Label ID="nameErrorLabel" runat="server" CssClass="redalert" Text="*complete name" Visible="False"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="boring">E-Mail</td>
            <td>
                <asp:TextBox ID="emailContactUsTextBox" runat="server" CssClass="boring" ToolTip="Enter Email" Width="443px"></asp:TextBox>
                <asp:Label ID="emailErrorLabel" runat="server" CssClass="redalert" Text="*complete email" Visible="False"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="boring">Enquiry</td>
            <td class="auto-style5">
                <asp:TextBox ID="enquiryContactUsTextBox" runat="server" CssClass="classic" Height="158px" ToolTip="Enter Enquiry" Width="441px"></asp:TextBox>
                <asp:Label ID="equiryErrorLabel" runat="server" CssClass="redalert" Text="*complete enquiry" Visible="False"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="auto-style5"></td>
            <td class="auto-style2"></td>
        </tr>
        <tr>
            <td class="smallheader" colspan="2">&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style5">&nbsp;</td>
            <td class="auto-style2">&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style5" colspan="2">
                <asp:Button ID="sendButton" runat="server" CssClass="smallheader" OnClick="sendButton_Click" Text="Send" ToolTip="Sending to NeoMan!" Width="200px" />
                <asp:Button ID="homeButton" runat="server" CssClass="smallheader" OnClick="homeButton_Click" Text="Home" ToolTip="Continue Shopping With NeoMan" Width="200px" />
                <asp:Button ID="resetButton" runat="server" CssClass="smallheader" OnClick="resetButton_Click" Text="Reset Form" ToolTip="Clear Form" Width="200px" />
            </td>
        </tr>
        <tr>

名称
电子邮件
询问

Microsoft已经有了buil ib验证器,您可以使用它来完成这类任务。要确保字段不为空,请使用所需的字段验证器,请参阅w3c学校网站上的此示例。 对于更复杂的验证,可以使用正则表达式验证器。请查看msdn网站()了解这一点,它非常简单。
您还可以研究JQuery验证器,它可以灵活地执行更有趣的验证。

您可以使用
TextChangedEvent
如下:

protected void nameContactUsTextBox_TextChanged(object sender, EventArgs e)
{
    nameErrorLabel.Visible = nameContactUsTextBox.Text == "";
}

并为每个文本框添加textchanged事件。

这是因为当标签不应该显示时,您需要隐藏标签,而不仅仅是隐藏标签。将
if
s更改为
if else
s,并添加一个摘要else条件

else
{
    nameErrorLabel.Visible = false;
    emailErrorLabel.Visible = false;
    equiryErrorLabel.Visible = false;
}
你应该使用 占位符
属性显示需要输入的值的类型,并在框失去焦点或单击按钮后使用jquery检查这些答案。如果您需要一些示例代码,请告诉我

您也可以发布您的aspx文件吗?您对jquery很满意吗?这可以通过使用带有id和错误检查条件的span标记轻松完成。您使用的是表单、MVC还是其他模式?有什么帮助吗?有什么问题吗?错误在哪里?您的代码似乎很好,问题出在哪里?在他的aspx文件中,他将这些错误标签的可见性设置为false