-6

如果输入的年龄低于 12 岁,我想要一个特定的文本。我想到了一些隐蔽的 int,以某种方式。但我不知道怎么做。有人可以帮我吗?

这就是我所拥有的:

<asp:TextBox ID="txtAge" runat="server" />
<br />
<asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />
<br />
<asp:Literal ID="litResult" runat="server" />

这是我的代码隐藏:

protected void btnSend_Click(object sender, EventArgs e)
{
    if (txtAge.Text <= 12)
    {
        litResult.Text = "You are a child";
    }
}
4

2 回答 2

0

您需要将 txtAge.Text 转换为整数然后执行此操作。

protected void btnSend_Click(object sender, EventArgs e)
    {
        int age = -2;
        try
        {
            age = int.Parse(txtAge.Text);

            if (age <= 12)
            {
                litResult.Text = "You are a child";
            }
        }
        catch (Exception e)
        {
            litResult.Text = "Entered values is not a number ";
        }
    }
于 2013-02-03T14:18:08.373 回答
0

a的Text属性TextBox是一个字符串,所以需要将 age 转换为 a int

protected void btnSend_Click(object sender, EventArgs e)
{
    int age;
    if (int.TryParse(txtAge.Text, out age));
    {
        if (age <= 12)
            litResult.Text = "You are a child";
    }
    else
        litResult.Text = "Please enter a valid age";
}
于 2013-02-03T14:25:48.143 回答