2013-02-03 1 views
-6

입력 한 나이가 12 세 미만인 경우 특정 텍스트를 갖고 싶습니다. 어떤 식 으로든 int에 숨겨진 것을 생각했습니다. 그러나 나는 어떻게 알아낼 수 없다. 누군가 나를 도울 수 있습니까?나이가 12 살 이하일 때 특정 텍스트를 원합니다.

이 내가 무엇을 가지고 :

<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

질문을 추가하는 것을 잊었습니다. –

답변

0

TextBoxText 속성은 문자열, 그래서 당신은 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"; 
} 
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 "; 
     } 
    } 
관련 문제