1

I have a dropdownlist in asp.net webform with long values as its options. Every time I select a value, it should be displayed on the dropdownlist field.

<asp:DropDownList ID="addressDdl" runat="server" CssClass="form-control" Width="210px">
 </asp:DropDownList>

I populate my dropdownlist using this method:

protected void Page_Load(object sender, EventArgs e) 
{
    List<address> addressAll = //get table data from SQL that has been returned as a list

    addressDdl.DataSource = addressAll;
    addressDdl.DataTextField = "address";
    addressDdl.DataValueField = "addressID";
    addressDdl.DataBind();
    addressDdl.Items.Insert(0,"--Select--");
}

However, since my dropdownlist is not long enough, it can only display parts of it. Is there any way so the dropdownlist is able to display the selected value in 2 lines?

Below is an example of the problem I face:

enter image description here

Kerzoz
  • 331
  • 5
  • 18
  • CSS size fixing in dropdownlist options may help: `select {width: n}` (n in pixels). Note that sometimes this is browser-dependent, another way to show full text with label inside `UpdatePanel` is available. – Tetsuya Yamamoto Jul 27 '17 at 06:35
  • I did try the `dropdownlist` size fixing, however, all it did was making the `dropdownlist` to have more place to fit stuff in, now actually warping the text so it moves to the next line @TetsuyaYamamoto – Kerzoz Jul 27 '17 at 06:38
  • Technically this isn't c# and it's not really asp.net either. You might get a wider spread of contributors if you tag it with HTML, css etc.. – Caius Jard Jul 27 '17 at 06:48
  • Is it a plain `select`, `asp:DropDownList` or AJAX toolkit combobox control? 2 former controls primarily uses CSS, but the latter has `DropDownExtender` alternative which able to display multi-line text option just by using `
    ` between texts.
    – Tetsuya Yamamoto Jul 27 '17 at 07:25
  • it is `asp:DropDownList` @TetsuyaYamamoto – Kerzoz Jul 27 '17 at 07:27
  • Which CSS styling currently used? I found interesting example here: http://jsfiddle.net/t0xicCode/454Lqbz8/. It uses `data-content` attribute which IMHO may be inserted from code-behind for each `asp:ListItem` you have. – Tetsuya Yamamoto Jul 27 '17 at 07:41
  • I'm currently using `form-control` css @TetsuyaYamamoto – Kerzoz Jul 27 '17 at 08:15
  • What method used for binding data to dropdownlist (using a `List` collection or using `DataTable` taken from database query like `SqlConnection`)? I need to clarify this before using specific HTML/CSS attributes to apply in code-behind. – Tetsuya Yamamoto Jul 27 '17 at 08:28
  • I'm populating the dropdownlist using a `List` that is taken from an `SqlConnection DataTable` I will update my post with my method of populating the dropdownlist for more clearance @TetsuyaYamamoto – Kerzoz Jul 27 '17 at 08:32

2 Answers2

1

Since you're using form-control CSS class, I'm sure that you're using Bootstrap CSS.

In Bootstrap, it is possible to implement multi-line options using data-content attribute for select element.

Note: For 2 first choices available below, ensure any long string value you want to show in data-content have <br /> tag inserted between them (see this example fiddle). The tag can be inserted by using String.Insert (suggested using additional property that stores wrapped value in address class):

foreach (var addr in addressAll)
{
    if (addr.address.Length >= [position_index])
    {
        // new property to store wrapped values to be displayed in data-content attribute
        addr.longAddress = addr.address.Insert([position_index], "<br />");
    }
}

1) To implement the custom attribute with asp:DropDownList server control, add it manually in code-behind after data binding. Here is the code-behind part:

protected void Page_Load(object sender, EventArgs e)
{
     List<address> addressAll = ... //get table data from SQL that has been returned as a list

     addressDdl.DataSource = addressAll;
     addressDdl.DataTextField = "address";
     addressDdl.DataValueField = "addressID";
     addressDdl.DataBind();
     addressDdl.Items.Insert(0,"--Select--");

     // before transform the list into array, use foreach loop provided above

     var arr = addressAll.ToArray();

     // note: zero-index skipped here
     for (int i = 1; i <= addressDdl.Items.Count; i++)
     {
         // add data-content attribute for select options
         addressDdl.Items[i].Attributes.Add("data-content", arr[i - 1].longAddress.ToString());
     }
}

2) As an alternative, a Repeater may be used to replace DropDownList with plain select element, but with ItemTemplate to set option attributes:

ASPX

<asp:Repeater ID="rptDeliveryAddress" runat="server">
    <HeaderTemplate>
        <select id="addressDdl">
            <option value="">--Select--</option>
    </HeaderTemplate>
    <ItemTemplate>
            <option data-content="<%# DataBinder.Eval(Container.DataItem, "[longAddress]") %>" value="<%# DataBinder.Eval(Container.DataItem, "[address]") %>"><%# DataBinder.Eval(Container.DataItem, "[address]") %></option>
    </ItemTemplate>
    <FooterTemplate>
        </select>
    </FooterTemplate>
</asp:Repeater>

3) Another way I know to use multi-line dropdown is manipulating width in CSS selector together with selectPicker:

ASPX

<asp:DropDownList ID="addressDdl" runat="server" CssClass="form-control selectpicker large-bootstrap-select" ... />

CSS

/* CSS Class */
.bs-container.large-bootstrap-select {
   .dropdown-menu {
      width: 100px; /* set this attribute with desired size in pixels */
      li a span.text {
         word-wrap: break-word;
         white-space: normal;
      }
   }
}

JS

$('#<%= addressDdl.ClientID %>').selectpicker();

References:

How to add a data-attribute to a dropdown menu with C#

Is it possible to have multi-line options?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

you can give static height and white-space: initial; to select

#wgtmsr{
 width:150px;  
 height: 50px;
 white-space: initial;
}

#wgtmsr option{
 width:100px;   
}
<select name="wgtmsr" id="wgtmsr">
<option value="kg">Kg</option>
<option value="gm">Gm</option>
<option value="pound">Pound</option>
<option value="MetricTon">Metric ton</option>
<option value="litre">Litre</option>
<option value="ounce">OunceOunceOunce OunceOunce OunceOunceOunce</option>
</select>
Supraja Ganji
  • 1,187
  • 1
  • 6
  • 8