好物!!Linq搭配AnonymouseType
一整個像在寫JavaScript
DropDownList 想 綁定 2 個欄位?
想要暫時的struct卻要建一個?
看下去唄
public partial class Test : System.Web.UI.Page
{
string id;
string firstName;
string lastName;
Object GetValueText()
{
List dataSource = new List();
dataSource.Add(new Test()
{
id = "1",
firstName = "first01",
lastName = "last01"
});
dataSource.Add(new Test()
{
id = "2",
firstName = "first02",
lastName = "last02"
});
var query = from q in dataSource
select new
{
value = q.id,
text = q.lastName + " "
+ q.firstName
};
var result = query.ToList();
result.Insert(0, new
{
value = "",
text = "請選擇"
});
return result;
}
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl = new DropDownList();
ddl.DataValueField = "value";
ddl.DataTextField = "text";
ddl.DataSource = GetValueText();
ddl.DataBind();
form1.Controls.Add(ddl);
}
}
有像在寫JavaScript唄 XD
沒有留言:
張貼留言