本文介绍了如何添加“选择项目”到使用LINQ to SQL填充的下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个下拉列表,我使用linq填充sql,它将显示美国州的列表。在下拉列表的顶部,我想要一个值为XX的选择状态条目。这是我第一次真正使用linq来sql我有点迷失。 这是我的代码到目前为止: var theStates = 来自州 in db.States 选择 new { StateID = states.StateID, StateName = states.StateName }; dropPrimaryAddressState.DataSource = theStates; dropPrimaryAddressState.DataTextField = StateName; dropPrimaryAddressState.DataValueField = StateID; dropPrimaryAddressState.DataBind(); 解决方案 var theStates = 来自状态 in db.States 选择 new { StateID = states.StateID, StateName = states。 StateName }; dropPrimaryAddressState.DataSource = theStates; dropPrimaryAddressState.DataTextField = StateName; dropPrimaryAddressState.DataValueField = StateID; dropPrimaryAddressState.DataBind(); ListItem newItem = new ListItem(); newItem.Text = 选择状态; newItem.Value = XX; dropPrimaryAddressState.Items.Insert( 0 ,newItem); dropPrimaryAddressState.SelectedIndex = 0 ; I have a drop down list that I am filling using linq to sql which will display a list of U.S. states. At the top of the drop down I would like to have a "Select a State" entry with a value of "XX". This being the first time I have really used linq to sql I am a little lost.Here is my code so far:var theStates = from states in db.States select new { StateID = states.StateID, StateName = states.StateName }; dropPrimaryAddressState.DataSource = theStates; dropPrimaryAddressState.DataTextField = "StateName"; dropPrimaryAddressState.DataValueField = "StateID"; dropPrimaryAddressState.DataBind(); 解决方案 var theStates = from states in db.States select new { StateID = states.StateID, StateName = states.StateName };dropPrimaryAddressState.DataSource = theStates;dropPrimaryAddressState.DataTextField = "StateName";dropPrimaryAddressState.DataValueField = "StateID";dropPrimaryAddressState.DataBind();ListItem newItem = new ListItem();newItem.Text = "Select a State";newItem.Value = "XX";dropPrimaryAddressState.Items.Insert(0, newItem);dropPrimaryAddressState.SelectedIndex = 0; 这篇关于如何添加“选择项目”到使用LINQ to SQL填充的下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 18:25