爲此,您需要攔截數據源返回的項目列表,然後手動將Coverage分配給下拉項目中的項目。
這裏是你的標記
<asp:LinqDataSource ID="EvaluationsData" runat="server"
ContextTypeName="Bonus.Models.BonusDataContext"
Select="new (Id, Name, Coverage)" TableName="BM_Evaluations"
onselected="EvaluationsData_Selected">
</asp:LinqDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="EvaluationsData" DataValueField="Id" DataTextField="Name"
ondatabound="DropDownList1_DataBound">
</asp:DropDownList>
這裏是你的代碼
List<object> evaluations;
protected void EvaluationsData_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
evaluations = new List<object>(e.Result as IEnumerable<object>);
}
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
foreach (ListItem item in DropDownList1.Items)
{
var eval = evaluations.Where(a => a.Id == item.Value).FirstOrDefault();
if (eval != null)
{
item.Attributes.Add("Coverage", eval.Coverage);
}
}
}