سؤال

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.

هل كانت مفيدة؟

المحلول

I do not think it not a good idea to do this type of validations on the server side before you have to consider client side validation I would suggest to you using a simple JavaScript function that would filter the second list and remove the duplicates and you can do it on document ready function since i think you reload the page every time you add record

$(document).ready(function() {
  var prev;
$('select[name=listBoxB] option').each(function() {
    if (this.text == prev)
     {
        $(this).remove();
     }
    prev= this.text;
});
});

نصائح أخرى

You must declare the select tag with the runat="server" in order to use the standard server side techniques. You can probably figure out the rest yourself, but I found http://www.aspnettutorials.com/tutorials/controls/htmlselect-control-csharp/ to be a fairly simple example doing the most common things you would need to do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top