Search code examples
jqueryasp.nettwitter-bootstrapbootstrap-select

Set selected values from server-side to bootstrap-multiple-select


For bootstrap-select I want to set selected values from server side. ( I work with ASP.Net). As I read, I should add to name of select [] to get multiple values. But ASP.Net doesn't give opportunity to make this. So I don't know how to make it.

I try just set my list of selected values as value to my select, but this doesn't help.

private void LoadPlatform(IEnumerable<string> platforms) {
  try {
    selectPlatforms.Value = string.Join(",", platforms);
  }
  catch (Exception ex) {
    UnityConfig.Container.Resolve<IExeptionLogger>().Log(ex);
  }
}

Solution

  • Add runat="server" to your select :

    <select id="Select1" runat="server" name="D1">
    
    </select>
    

    Then you can add datasource the normal way:

     Select1.DataSource = platforms; // or a dataset 
     Select1.DataTextField = "name"; 
     Select1.DataValueField = "id";
     Select1.DataBind();
    

    The other way would be to call a webmethod and return Json then populate it by Jquery :

      $.each(data, function (key, val) {
            items += "<option value='" + val.id+ "'>" + val.value+ "</option>";    
      });    
      $('#Select1').html(items);
    

    if you want to set the default selected item:

    ListItem li = Select1.Items.FindByText("Three");
    ListItem li = Select1.Items.FindByValue("3");
    li.Selected = true;
    

    source