我正在使用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 :(得分:0)
在html下拉声明中添加此项,并将'AppendDataBoundItems'属性设置为true。见下文......
<asp:DropDownList ID="dropdownsummary" runat="server" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="-1" Text="All"></asp:ListItem>
</asp:DropDownList>
答案 1 :(得分:0)
使用:
dropdownsummary.DataBind();
而不是:
DataBind();
答案 2 :(得分:0)
试试这个
dropdownsummary.DataValueField = "SUBID";
dropdownsummary.DataTextField = "SUBValue";
dropdownsummary.DataSource = binmy;
dropdownsummary.Items.Insert("0", new ListItem ( "All" , "0" ));