Thursday, April 12, 2012

how to get the order of dynamically created dropdownlists

I've created some dropboxlists using JavaScript, asp.net. So a user can add as many dropboxlists as he wants by clicking a + button and removing them by clicking a - button.
If it's hard to understand what I mean pls see (http://stackoverflow.com/questions/10101262/how-to-implement-a-list-of-dropboxes-in-c-sharp).
And now I'd like to implement the code behind and want to define the order of the dropboxes, but I don't know which one is my first dropbox etc.
We assume that all asp:dropdownlist contain the following for list elements: method1, method2, method3 and method4. If a user selects an element a method in the codebehind is implemented.


example:
dropboxlist1: select list item method2,
dropboxlist2: select list item method1,
dropboxlist3: select list item method3,


string txt= "";
 if (dropboxlistID.Text == "method1"){
   txt = method1Imp();
 } else if (dropboxlistID.Text == "method2") {
   txt = method2Imp();
 } else if (dropboxlistID.Text == "method3") {
   txt = method3Imp();
 } else {
 }
 


But at this moment I don't have any idea which dropboxlist came first and which method should be performed on my string first. Thanks!



Answer:


Try enqueueing each method into a queue as a delegate, then draining (invoking each delegate) the queue once you're ready from a single thread. This will ensure that the order of execution matches the order of user choices.
Sorry I didn't initally include code. Here's a basic example to get you started:
Queue<Func<string>> actions = new Queue<Func<string>>();
if(dropboxListID.Text =="m1")
{
 actions.Enqueue(method1Imp);
}
if(dropboxListID.Text = "m2")
{
 action.Enqueue(method2Imp);
}
...
Sometime Later when you're ready to process these...
string txt = "";    while(actions.Count >0)
{
 var method = actions.Dequeue();
 txt = method();
}
Here's a blog post that delves further into the concept of a work/task queue:

No comments:

Post a Comment