Simon Price
Simon Price

Reputation: 3261

asp.net dropdown list with checkboxes

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.

Upvotes: 2

Views: 3202

Answers (2)

ravi zinzuwadia
ravi zinzuwadia

Reputation: 24

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.

Upvotes: 1

meh-uk
meh-uk

Reputation: 2151

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.

Upvotes: 0

Related Questions