Thursday, 22 August 2013

How do I retrieve / compare items in an HTML Listbox using C#

How do I retrieve / compare items in an HTML Listbox using C#

This must be easy... but I'm stuck just the same.
Here's the problem:
In an ASP.net project, created perhaps way before any of the new standards
were invented, two HTML List Boxes are present, let's call them listBox A,
and listBox B. By HTML, I mean they use HTML tags, not ASP ones:
<select name="listBoxB" id = "listBoxB" size="15" >
<option>--------Selected Approvers--------</option>
<%
SQLDataFactory.WorkflowApproversGroupDetails SQLDFSelectedApprovers = new
SQLDataFactory.WorkflowApproversGroupDetails();
DataTable dt1 = SQLDFSelectedApprovers.GetApproversGroupDetails(Request);
foreach (DataRow row1 in dt1.Rows)
{
if (row1["ID"].ToString().Equals(Request.Form["listBoxB"]))
Response.Write("<option selected value='" + row1["ID"].ToString() +
"'>" + row1["EMPNONAME"].ToString() + "</option>");
else
Response.Write("<option value='" + row1["ID"].ToString() + "'>" +
row1["EMPNONAME"].ToString() + "</option>");
}
dt1.Dispose();
%>
</select>
listBox B only really fetches data from the database.
listBox A contains a more complete set of data (also fetched from the
database, a different table). The form allows users to transfer data from
A to B, via Add and Remove buttons. The Add and Remove buttons simply use
insert, or delete statements. This works fine.
However, there is a bug. Namely, you can add data from listBox A to
listBox B more than once. This should not be allowed.
My logic for a proposed solution goes like this:
Upon the press of the Add button, a loop will run through all the items of
listBox B. If it already contains the item about to be added, it won't add
anything to the database, and thus, listBox B will not display anything
new. Otherwise, it will add the item to the database, which will also
update listBox B's list. Remove stays as is.
Sounds easy simple and enough, eh? I hit some snags.
How do I retrieve both of the list box items (selected item in A, all
items in B)?
Do I do a Request.Form["listBox B"], run it through a For loop, and add it
to an array? For that, I'll probably need the listBox B item count as
well. How do I get these things?
Also, no LINQs. I'm only allowed to use .NET 2.0, so I guess I'm going to
have to find other means of comparing the selected item from A to the list
of items from B. I'm looking at the following:
int index = Array.FindIndex(itemB, delegate(string s) { return
s.Equals(test); });
Where itemB is the array of items from listBox B, and s is the selected
item from listBox A.
Thanks.

No comments:

Post a Comment