2

I am working with an asp.net website and I need to use a drop down list with checkboxes.

I have this written in HTML that works, however I want a more elegent solution in getting the values to the code behind and retaining the values other than using Request.Form()

The code I am using to generate the drop down list currently and return the information is as below.

 <div class="btn-group">
      <a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="#">Report Status <span class="caret"></span></a>
        <ul class="dropdown-menu">
            <asp:PlaceHolder ID="statusSelectorPlaceHolder" runat="server"></asp:PlaceHolder>
        </ul>
 </div>

The Html I am generating is being created like this;

Private Shared Function BuildHtmlStatusSelector(dictOfStatus As Dictionary(Of String, String)) As StringBuilder
        Dim html As StringBuilder = New StringBuilder

        For Each item In dictOfStatus
            html.AppendLine(String.Format("<li style='padding-left: 10px'><label class='small' tabindex='-1'><input type='checkbox' id='reportStatus' name='reportStatus' checked='true' value='{0}'/>{1}</label></li>", item.Value, item.Key))
        Next
        Return html
End Function

When the page posts back I am checking or the values here like so

If Not Request.Form("reportStatus") = Nothing Then
            StatusCodes = Request.Form("reportStatus")
End If

its not the most elegant solution and I was hoping that there is a simple and effective asp.net control that i could implement instead so that I dont have to use this method

Im happy with either vb or c# examples as can work with both.

Any and all help would very much be appreciated.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • possible duplicate of [Multi-select dropdown list in ASP.NET](http://stackoverflow.com/questions/774587/multi-select-dropdown-list-in-asp-net) – meh-uk Sep 16 '15 at 10:35

2 Answers2

1

I will suggest you very simple way for this.

1). Your html based check boxes are fine.

2). Just add one JavaScript event onClick="functionName('labelValue')" into it and pass value of label into it.

3). Add server side hidden field on your form.

4). Now every time function called append value send as parameter and seperator like "," to value of serer side hidden field.

5). Now when on button when page is submitted you will get value server side in hidden field.

6). Split hidden field value with .split(',') and you will get all value in array.

0

I have personally used the jQuery plugin chosen to wrap a normal select box with an elegant UI which allows you to select multiple items.

It even performs well with pretty large lists of items and has search etc.

meh-uk
  • 2,031
  • 1
  • 23
  • 36
  • im not fully upto speed with JQuery, but am soon funding a course to help me be more confident with it. How would i then pass the selected values to the code behind – Simon Price Sep 16 '15 at 10:32
  • Actually it looks like your question is a duplicate of http://stackoverflow.com/questions/774587/multi-select-dropdown-list-in-asp-net – meh-uk Sep 16 '15 at 10:34
  • which ever solution i use, i still need help in passing the selected values back to the code behind – Simon Price Sep 16 '15 at 10:46
  • See http://stackoverflow.com/questions/18924147/how-to-get-values-of-selected-items-in-checkboxlist-with-foreach-in-asp-net-c for that. – meh-uk Sep 16 '15 at 10:51
  • thats for an ``, if the drop down list is being build and populated by JQuery the last link is not of much use – Simon Price Sep 16 '15 at 11:01
  • If you're using the jQuery plugin you'd still need to create a select box on the page to wrap - probably the best way to do that is to use asp:listbox or something to create a select box - then you can use the standard asp.net serverside code to read the content there. – meh-uk Sep 16 '15 at 11:20