2014-05-01 9 views
8

Ho il seguente metodo Page_Load nel mio controllo (System.Web.UI.UserControl):Come popolare l'elenco a discesa prima che la pagina venga caricata nei moduli Web?

protected void Page_Load(object sender, EventArgs e) 
    { 
     DropDownList ShowAssumptions = new DropDownList(); 
     List<string> list = new List<string>() 
     { 
      "test", 
      "test2" 
     }; 
     ShowAssumptions.DataSource = from i in list 
            select new ListItem() 
            { 
             Text = i, 
             Value = i 
            }; 
     ShowAssumptions.DataBind(); 
    } 

Poi, nel mio aspx ho questo:

<asp:DropDownList id="ShowAssumptions" runat="server"> 
</asp:DropDownList> 

Ma, DropDownList mai viene popolato. Che cosa sto facendo di sbagliato?

+0

prova invece: ShowAssumptions.DataSource = list; – MaxOvrdrv

+0

@MaxOvrdrv Grazie, ma non sembra funzionare. – user1477388

+1

oh no ho appena notato: non dichiarare il dropdownlist nel caricamento della pagina ... basta fare riferimento usando questo. Vedi la mia risposta qui sotto ... – MaxOvrdrv

risposta

8

Basta assegnare l'elenco come origine dati. Suppongo anche che tu non voglia ricaricare la lista su ogni 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(); 
    } 
} 
+0

Ah, capisco. Grazie! Sembra che il mio problema fosse 'DropDownList ShowAssumptions = new DropDownList();'. Dopo averlo estratto, funziona. – user1477388

+0

Non è necessario creare controlli quando si è già creata la versione asp di esso. Scusa, non so in che altro modo di esprimere la cosa. – Tsukasa

1
protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      foreach (string item in list) 
      { 
       ShowAssumptions.Items.Add(item); 
      } 
     } 
    } 
+0

Grazie mille, ma sembra che non funzioni ancora. Qualcos'altro che posso provare? – user1477388

+0

@ user1477388 sei sicuro? funziona per me, – Sajeetharan

2
protected void Page_Load(object sender, EventArgs e) 
    { 
     //Don't do this here! 
     //DropDownList ShowAssumptions = new DropDownList(); 

     List<string> list = new List<string>() 
     { 
      "test", 
      "test2" 
     }; 
     this.ShowAssumptions.DataSource = from i in list 
            select new ListItem() 
            { 
             Text = i, 
             Value = i 
            }; 
     this.ShowAssumptions.DataBind(); 
    } 
3

Nel caso in cui si utilizza ASP.NET WebForms, EF e Bootstrap provare questo

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; } 
} 
+0

Un buon esempio quando si usa Elenco di oggetti. – FailedUnitTest