2013-12-19 54 views
0

我使用LINQ從數據庫獲取來填充使用下面的代碼從下拉列表中選擇asp.net值:如何在包括人口下拉列表

XXXDataContext summary = new XXXDataContext(); 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     var binmy = (from bin in summary SUBPRODUCTs 
        order by bin.SUBID 
        select new { bin.SUBID, bin.SUBValue } 
       );      
     dropdownsummary.DataValueField = "SUBID"; 
     dropdownsummary.DataTextField = "SUBValue"; 
     dropdownsummary.DataSource = binmy; 
     DataBind(); 
    } 
} 

在這裏,我想包括「所有'作爲默認值如何做到這一點?

回答

0

在html下拉聲明中添加此項目,並將'AppendDataBoundItems'屬性設置爲true。見下文......

<asp:DropDownList ID="dropdownsummary" runat="server" AppendDataBoundItems="true"> 
    <asp:ListItem Selected="True" Value="-1" Text="All"></asp:ListItem> 
</asp:DropDownList> 
0

用途:

dropdownsummary.DataBind(); 

相反的:

DataBind(); 
0

嘗試這個

dropdownsummary.DataValueField = "SUBID"; 
dropdownsummary.DataTextField = "SUBValue"; 
dropdownsummary.DataSource = binmy; 
dropdownsummary.Items.Insert("0", new ListItem ("All" , "0"));