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

时间:2016-05-06 04:01:01

标签: c# asp.net webforms

如果用户选择“其他”单选按钮并将其留空或在文本框中输入错误数据,则需要显示错误。一旦用户输入正确的数据错误消失。

<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>

下图显示了radiobutton如何在文本框旁边显示文本框和错误消息标签。 Here is image of radiobutton display

#####这是我的尝试
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;
                }

2 个答案:

答案 0 :(得分:1)

试用此代码: -

<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>

和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 = "";
        }
    }

答案 1 :(得分:0)

试试这个 这是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;
        }
    }

这里是

<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>