Last post Dec 13, 2007 05:24 PM by pinky8
Dec 12, 2007 01:11 PM|pinky8|LINK
When my page is postback, it fills a listbox with data from a datatable based on some input from another control. And it marks items 1 and in the listbox as selected based on whatever was saved in the database before that. The datatable gets data from Oracle
database. After the page finishes with rendering its contents, then, user unselects all those items 1 and 2 and selects other items 5 and 6. I try to get the NEWLY selected items 5 and 6 and save that to the database. The problem I encounter is it gets the
OLD items 1 and 2 if I use ListBox1.Items[i].Value. So, I use HttpContext.Current.Request.Form[ListBox1.UniqueID] which gets the first NEWLY selected item 5 but it does not get item 6. It keeps on getting item 5 every time it gets into the loop.
Since Request.Form collection has no object model to yield things like SelectedItem, how can I check to see if an item is selected from the code below. Please note that I cannot use if (ListBox1.Items[i].Selected) because that will check the OLD value.
In summary my questions are:
1) How can I replace this IF statement "if (ListBox1.Items[i].Selected)" with HttpContext.Current.Request.Form so that it checks on the NEWLY selected items? Remember that ListBox1.Items[i].Selected does not work because it checks on the OLDly selected
2) How can I use HttpContext.Current.Request.Form to get all NEWLY selected items in the loop?
My code below does not give correct result:
for (int i = 0; i < ListBox1.Items.Count; i++)
if (ListBox1.Items[i].Selected) //NEED TO CHANGE THIS LINE TO HttpContext
sTesting += ListBox1.Items[i].Value; //this gets the old value.
string strNewVal = string.Empty;
strNewVal += HttpContext.Current.Request.Form[ListBox1.UniqueID]; //this gets the new value.
Dec 12, 2007 01:52 PM|novice_dlh|LINK
StringBuilder newValue = new StringBuilder();
foreach(ListItem item in ListBox1.Items)
hope this helps
Dec 12, 2007 02:00 PM|novice_dlh|LINK
You don't have to use 'HttpContext.Current.Request.Form[ListBox1.UniqueID]'
in a loop. It returns a comma separated values of the selected items in the list box.
I tried your code and I am getting the newly selected values.
Dec 12, 2007 03:16 PM|pinky8|LINK
Am I doing something wrong here? It doesn't work for me, very strange. I even try the stringbuilder suggestion and it also gets the OLD selected items -- it doesn't even get the NEW selected item. I'm thinking may be it has to do with postback, but the postback
was finished and every thing was rendered before user select other items so the problem shouldn't have anything to do with postback.
May be I should try to fix the codes that I posted earlier, so can you please answer the two questions that I had?
Dec 13, 2007 05:24 PM|pinky8|LINK
Thank you all for trying to help. I had to use Request.Form to make it work.