How to populate dropdown list before page loads in webforms?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
foreach (string item in list)
{
ShowAssumptions.Items.Add(item);
}
}
}
In case if you use ASP.NET WebForms, EF and Bootstrap try this
HTML
<div class="form-group">
<label class="control-label" for="inputType">Lines: </label>
<asp:DropDownList ID="DropDownListFabricLines" CssClass="dropdown form-control" runat="server"></asp:DropDownList>
</div>
C#
var entities = new DababaseEntities();
List<FabricLineView> fabricLines = entities .Line.Select(x=> new FabricLineView { ID = x.LineaID, Name = x.LineaNombre }).ToList();
DropDownListFabricLines.DataValueField = "ID";
DropDownListFabricLines.DataTextField = "Name";
DropDownListFabricLines.DataSource = fabricLines;
DropDownListFabricLines.DataBind();
public sealed class FabricLineView
{
public int ID { get; set; }
public string Name { get; set; }
}
Just assign the list as the datasource. Also I assume you don't want to reload the list on every PostBack.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> list = new List<string>()
{
"test",
"test2"
};
ShowAssumptions.DataSource = list;
ShowAssumptions.DataBind();
}
}