1

I am using do-while loop for getting each items for the list box.

My code sample is like below:

        do
        {
            string nameOfPersonFromWeb;

            //Here is code that do some task from web 
            //for eg. get the name list from the website...

            ListBox.Items.Add(nameOfPersonFromWeb);

            totalnumber++;
        } while (totalNumber<=25);

My problem is that, I am not getting each item instantly. When the loop is ended, it is displaying the whole items.

I want to display the name in each loop in the list box. I also want to highlight the particular listbox items by sending listbox item's index. I am using C# in code behind and Asp.net.

8
  • @DJ KRAZE: Because atleast one time I have to enter the loop. Commented Jan 10, 2012 at 17:50
  • you should use a for loop also please post the portion of the code where you are assigning nameOfPersonFromWeb Commented Jan 10, 2012 at 17:50
  • if you have to enter at least one time I'd like to see where you are assigning the nameOfPersonFromWeb.. can you paste the full method.. Commented Jan 10, 2012 at 17:51
  • Why not populate your collection and then set the ListBox.ItemsSource to the new collection instead? Commented Jan 10, 2012 at 17:52
  • @DJ KRAZE: I can not share other code as it is confidential. My problem is just as I stated above in my question. All other portion of my code is fine. Commented Jan 10, 2012 at 17:54

4 Answers 4

2

It seems you are expecting the same behavior as in Winforms.

Asp.Net works with postbacks. It will create the hole page and then "send it back to the browser" on each event. That's why your items are populated all at once. If you want some "one-by-one" behaviour, you'll need to do it in the browser, using javascript (if the logic needs to be in the server, then you'll need to use ajax).

I don't know what are you exactly trying to achieve so I can't help more.

Sign up to request clarification or add additional context in comments.

Comments

0

You just need to add a new ListItem to your collection. The usage you're using is default behavior and adds a ListItem by name. You can use an overloaded ListItem constructor to set both the name and the value of the ListItem.

    do
    {
        string nameOfPersonFromWeb;

        //Here is code that do some task from web 
        //for eg. get the name list from the website...

        ListBox.Items.Add(new ListItem(nameOfPersonFromWeb, totalNumber));

        totalnumber++;
    } while (totalNumber<=25);

4 Comments

It is displaying whole names after the loop is ended, not as I want.
I'm not sure what you're trying to do then. Can you update your question with what you'd like to have happen? Based on the code snippet you provided, I'm not sure what your end goal is.
The name is extracted from some xyz website in each loop. My problem is that, I am not able to display the name as it is extracted. After the whole loop is finished, my list box item is filled with the items. I want to display each name instantly as it is extracted from that website.
OK - I agree with @ahmadali then - what you're trying to do should be done client-side instead of server-side (which your question/code was demonstrating).
0

ASP.net Is server side and you haven't any control in local code on it .You should use ajax if there is any way.

2 Comments

Can you provide me some examples or hints in which ajax is used to do such task as mine?
I didn't work with ajax until now.and I saw if there is any way.but I don't know any way.maybe Ajax controls can do this for you
0

You will not be able to accomplish this unless you do this Asynchronously if what you want is the page to load before showing the names. But as ahmadali mentioned, this in the server, so everything will be Rendered after it is done loading.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.