0

I want to print all the numbers in the loop .....
How should I do it ?
Please help !

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat ="server" >
void Page_load()
{
  int counter = 0;
  while (counter <= 10)
     {
       messageLabel.Text = counter.ToString();
       counter++;
     }
}
</script>  

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Loops with C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID = "messageLabel" runat = "server"/>
    </div>
    </form>
</body>
</html>
7
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by dnanetwork
0

The ToString() method is applicable to objects and not to data types. Make use of CType instead and you should be good.

0

It would probably be easier if you declared a string and then moved it after the loop. I also declared a string variable so you don't have to mess with messageLabel and object notation inside the loop.

This will print all of the numbers between 0 and 10 (inclusive).

int counter = 0;
string str; 
while (counter <= 10)     {       
  str += counter.ToString() + ',';  
  counter++;     
}
str.Remove(str.Length - 1, 1);
messageLabel.Text =str;

Edited by svilla: n/a

0

while (counter <= 10)
{
messageLabel.Text = counter.ToString();
response.write(counter)
counter++;
}

0
int counter = 0; 
string append="";
while (counter <= 10)  
   {      
      if  append=""
           append = counter.ToString();   
      else
           append =append & "," & counter.ToString(); 
      end if  
      counter++;  
   }
messageLabel.Text =append;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.