Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All

I am appending records in textbox which is in Multiple mode for example see below code:

On Button Click event :

for (int i = 0; i <100; i++)
{
  TextBox1.Text += i.ToString() + "\n";
}

but what happens now it displayed all 100 records once event completed, I want like when i=1 so it adds 1 in textbox so on .aspx page it will display 1 then 2 then 3 like one by one so how can i do that.

Thanks


[Edit]Code is wraped in Pre tags[/Edit]
Posted
Updated 21-Dec-10 2:08am
v5
Comments
JF2015 21-Dec-10 8:07am    
Edited to improve code formatting.

Hi,

try this:
C#
for (int i = 0; i < 100; i++)
{
  TextBox1.Text += i.ToString() + Environment.NewLine;
}


or if you want to increase only once per click:

C#
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
  i++;
  TextBox1.Text += i.ToString() + Environment.NewLine;
}
 
Share this answer
 
v2
You want that your UI get updated one by one.Why are you not doing with javascript using settimeout.
Although, you can also invoke ajax call to get the data from server, using settimeout and update the UI one by one.

[Added]

Its better, if you do this at client side. But for server side also
you need to use setTimeout( initiateAjax(), timeout ) which
is a javascript method. initiateAjax() will initiate ajax call and get the data from server and update the UI, timeout is in miliseconds. Means initiateAjax() will be called after timeout miliseconds. So you need to do looping over it.
And about ajax call, you can use jQuery call, as I have used in my below article.
Client Templating with jQuery


For details about setTimeout, have a look
Click here[^]

[/Added]
 
Share this answer
 
v2
Comments
Prashant B. Lavate 21-Dec-10 8:22am    
Will u please show me example ....
Brij 21-Dec-10 8:36am    
do you want through javascript i e client side or server side?
Prashant B. Lavate 21-Dec-10 8:51am    
i want from server side.please give me some sample code if possible
Brij 21-Dec-10 9:12am    
Added in my answer.
Instead of having the entire for loop in your button click event, I think you want the following...

This logic will help you display which iteration you are currently in. It will increment the value of "i"(_currentIValue) each time you press the button and the value in the textbox will reflect the current state of "i"(_currentIValue) at the end of each execution of this method.

C#
private int _currentIValue = 0;

protected void buttonClickHandler(object sender, EventArgs e)
{
    // Do your processing here...
    // Since you have your loop 0 to 100 in your example
    if( _currentIValue < 100 )
    {
        //TextBox1.Text = _currentIValue.ToString(); // This line will replace the text
        TextBox1.Text = TextBox1.Text + Environment.NewLine + _currentIValue.ToString();
        _currentIValue ++;
        // Can also be written in one line
        // TextBox1.Text = _currentIValue++;
    }
}
 
Share this answer
 
v5
Comments
Prashant B. Lavate 21-Dec-10 8:59am    
but how can i show on browser that it is appending one by one ?
fjdiewornncalwe 21-Dec-10 9:02am    
TextBox1.Text = _currentIValue.ToString() will do that for you in the loop.
Try this code snippet in your project and run it. It should make sense then.
Prashant B. Lavate 21-Dec-10 9:03am    
not working will u please put all code together its not working :(
fjdiewornncalwe 21-Dec-10 9:17am    
I have updated the code to append the new value if you want that.
fjdiewornncalwe 21-Dec-10 9:59am    
So why the one vote? According to your question, this is a perfectly appropriate answer. If this isn't the answer you want, perhaps you could update your question to ask what you really want. Blanket voting all the answers 1 is a little harsh and will likely drive people away from even bothering with any of your future questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900