简体   繁体   English

使用asp.net webform中的文本框处理radiobutoon的错误处理

[英]Error handling for radiobutoon with textbox in asp.net webform

I need to display error if user selects "Other" Radio button and leaves it blank or enters wrong data in textbox. 如果用户选择“其他”单选按钮并将其留空或在文本框中输入错误数据,则需要显示错误。 once user enters right data error goes away. 一旦用户输入正确的数据错误消失。

<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" OnSelectedIndexChanged="radyears_SelectedIndexChanged" Font-Names="Arial" Font-Size="12pt">
                <asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
                <asp:ListItem Value="30">30 Years</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
                </asp:RadioButtonList>
            <asp:TextBox ID="txtother" runat="server" AutoPostBack="True" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10"></asp:TextBox>
            <asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>

Below is image showing how radiobutton shows with textbox and error message label next to textbox. 下面的图像显示了radiobutton如何显示文本框和文本框旁边的错误消息标签。 Here is image of radiobutton display 这是radiobutton显示的图像

##### Here is my Try #####这是我的尝试
 if (radyears.SelectedValue == "Other") { if (String.IsNullOrEmpty(txtother.Text) || (!double.TryParse(txtother.Text, out years))) { lblothererror.Text = "Not Valid Input"; return; } else { lblothererror.Text = "valid number"; return; } 

Try this code :- 试试这个代码: -

<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" Font-Names="Arial" Font-Size="12pt" OnSelectedIndexChanged="radyears_SelectedIndexChanged">
                <asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
                <asp:ListItem Value="30">30 Years</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
                </asp:RadioButtonList>
            <asp:TextBox ID="txtother" runat="server" AutoPostBack="True" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10" OnTextChanged="txtother_TextChanged"></asp:TextBox>
            <asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>

and in aspx.cs page 并在aspx.cs页面中

protected void radyears_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (radyears.SelectedValue == "Other")
        {
            lblothererror.Text = "Enter age in Textbox";
        }
        else
        {
            lblothererror.Text = "";
        }
    }
    protected void txtother_TextChanged(object sender, EventArgs e)
    {
        if (radyears.SelectedValue == "Other")
        {
            double years = 100;
            if (String.IsNullOrEmpty(txtother.Text) || (Convert.ToInt32(txtother.Text) > years))
            {
                lblothererror.Text = "Invalid";
            }
            else
            {
                lblothererror.Text = "valid";
            }
        }
        else
        {
            lblothererror.Text = "";
        }
    }

Try this Here is c# code 试试这里这是c#代码

protected void radyears_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (radyears.SelectedValue == "Other")
        {
            lblothererror.Text = "Enter value in Textbox please";
        }

    }

    protected void txtother_TextChanged(object sender, EventArgs e)
    {
        if (radyears.SelectedValue == "Other")
        {
            double years = 123;//Any Value
            if (String.IsNullOrEmpty(txtother.Text) || (!double.TryParse(txtother.Text, out years)))
            {
                lblothererror.Text = "Not Valid Input";
                return;
            }
            else
            {
                lblothererror.Text = "valid number";
                return;
            }
        }
        else
        {
            lblothererror.Text = "";
            return;
        }
    }

and here it is 而且在这里

<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" OnSelectedIndexChanged="radyears_SelectedIndexChanged" Font-Names="Arial" Font-Size="12pt">
                <asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
                <asp:ListItem Value="30">30 Years</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
                </asp:RadioButtonList>
            <asp:TextBox ID="txtother" runat="server" OnTextChanged="txtother_TextChanged" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10"></asp:TextBox>
            <asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM