我有一个ComboBox如下设置,其中KVPList是IList(KeyValuePair如果重要的话):
comboBox.DisplayMember = "Value";
comboBox.ValueMember = "Key";
comboBox.DataSource = KVPList;
Run Code Online (Sandbox Code Playgroud)
然后我设置了绑定SelectedValue,绑定到BindingSource(到a DataSet).无论出于何种原因,在显示表单时,组合框始终显示为空白.但是它已正确填充(IList显示的值很好并且可以选择).
现在,我已尽力追踪,看起来最初在绑定时正确设置了SelectedValue,但随后它被重置为某个地方null.我玩过订单的东西也被调用,无济于事.
任何人都可以对此有所了解或建议解决方法吗?
对于记录,在同一个表单上,我ComboBox在同一个表单上有另一个表单,它的SelectedValue绑定是相同的BindingSource.这DataSource是一个DataSet,而不是一个IList,它就像一个魅力.这可能是做出一种选择DataTable从IList,但它似乎是一大堆额外的开销; 我是IList从枚举中生成的.
我有两个项目.一个是工作,另一个不是,但他们之间的差异并不是我认为"应该"具有任何重要性.第一个项目是破坏的项目,它是我想要解决的项目.第二个项目是我在第一个项目根本无法工作时创建的一个小样本项目.当然样本效果很好.
这是第一个项目的视图.我删除了一堆"MainWindowTabControlStyle",因为它只是破坏的组合框.我有理由确定问题不在风格中,因为它是来自正在运行的项目的复制和粘贴.
<Grid>
<TabControl Style="{DynamicResource MainWindowTabControlStyle}">
<TabItem Header="Tab 1"/>
<TabItem Header="Tab 2"/>
</TabControl>
</Grid>
<Style x:Key="MainWindowTabControlStyle" TargetType="{x:Type TabControl}">
...
<ComboBox
HorizontalAlignment="Right"
VerticalAlignment="Top"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Subscriptions, Mode=Default}"
SelectedItem="{Binding Path=SelectedSubscription, Mode=OneWayToSource}"
ItemTemplate="{DynamicResource SubscriptionsItemTemplate}"/>
...
</Style>
<DataTemplate x:Key="SubscriptionsItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=DisplayName, Mode=Default}"/>
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
以下是设置为MainWindow的DataContext的视图模型.ViewModelBase类与Josh Smith在本月的MSDN文章中写的完全相同.
public sealed class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
}
private ObservableCollection<Subscription> subscriptions;
public ObservableCollection<Subscription> Subscriptions
{
get
{
if (subscriptions == null)
{
subscriptions = new ObservableCollection<Subscription>();
subscriptions.Add(new Subscription() { DisplayName = "ABC" …Run Code Online (Sandbox Code Playgroud) 我正在使用mvc3,我的视图中有一个下拉列表.
@Html.DropDownListFor(m => m.State,
new SelectList(Model.StateList, "Value", "Text"))
Run Code Online (Sandbox Code Playgroud)
有没有办法在视图中设置所选值?
在我的Gridview中,我有以下模板字段:
<asp:TemplateField HeaderText="Dept Code" SortExpression="DeptCode">
<ItemTemplate>
<%# Eval("DeptCode") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlDeptCode" runat="server"
SelectedValue='<%# Eval("DeptCode") %>'
DataSource='<%# GetAllDepartments() %>'
DataTextField="DeptCode"
DataValueField="DeptCode" />
</EditItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
当我在一行上单击“编辑”时,此方法非常有用,它会用所有值填充DropDownList并为该行选择正确的值。
但是,当我尝试更新该行时: OnRowUpdating="UpdateRow"
protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = UserGV.Rows[e.RowIndex];
DropDownList ddl = row.FindControl("ddlDeptCode") as DropDownList;
string deptCode = ddl.SelectedValue;
}
Run Code Online (Sandbox Code Playgroud)
它找到DropDownList控件,但SelectedValue始终是一个空字符串。
我需要访问选定的值才能保存到数据库。
关于如何在后面的代码中的Gridview中获取DropDownList的SelectedValue的任何想法?
编辑:
您还可以从后面的代码填充DropDownList和SelectedValue:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
var deptMgr = new DepartmentMgr();
List<Department> …Run Code Online (Sandbox Code Playgroud) <CombobBox x:Name="cbo"
Style="{StaticResource ComboStyle1}"
DisplayMemberPath="NAME"
SelectedItem="{Binding Path=NAME}"
SelectedIndex="1">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=NAME}"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
在Window OnLoaded事件中,我编写了代码来设置ItemsSource:
cbo.ItemsSource = ser.GetCity().DefaultView;
Run Code Online (Sandbox Code Playgroud)
在加载窗口时,我可以看到最初正在加载第一个项目,但同时它清除了显示的项目。我被困在这种情况下,任何帮助表示赞赏。
问候
基肖尔
我正在尝试找到一个简单的Selenium调用来从选择下拉列表中获取当前选项.我知道有些调用会获取列表中的所有值,但我想知道当前选择了哪个选项.抱歉,如果这是微不足道的,但谷歌和Selenium IDE没有帮助我.谢谢.
我有一个连接到ObjectDataSource的DropDownList.在我的页面加载中,我根据具体情况将selectedvalue设置为特定值.如何在我的方法中选择索引以"重置"所选值,以便您仍然可以从其他选项中进行选择?或者我不应该使用selectedvalue来设置下拉列表的默认值?
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="ObjectDataSource" DataTextField="Type" DataValueField="TypeId"
AutoPostBack="true" onselectedindexchanged="DropDownList_SelectedIndexChanged" >
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
int default = category.TypeId;
DropDownList.SelectedIndex = default;
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
int id = int.Parse(DropDownList.SelectedValue);
Session["id"] = id;
}
Run Code Online (Sandbox Code Playgroud) 鉴于HTML:
<select id="mySelect">
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
Run Code Online (Sandbox Code Playgroud)
和javascript:
var e = document.getElementById('mySelect');
Run Code Online (Sandbox Code Playgroud)
要获得我可以使用的选择的值e.value和e.options[e.selectedIndex].value.
我知道e.options[e.selectedIndex].value会给我选择的值(1,2或3),并e.options[e.selectedIndex].text根据选择的内容给我test1,test2,test3.
可以使用e.value吗?这是旧浏览器中的问题吗?
哪个更正确:e.valuevs e.options[e.selectedIndex].value?
我认为应该是一个直截了当的问题.我有一个带有FormTemplate编辑和启用AJAX的RadGrid.FormTemplate中的一个字段是一个充满美国州选择的RadComboBox.我可以将RadComboBox绑定到数据源以填充所有项目,但我无法设置SelectedValue属性.
单击RadGrid上的行的"编辑"按钮时,将加载此RadComboBox.使用自定义FormTemplate,并通过AJAX加载正在编辑的行的内容.
我一直在寻找这个答案.在这里查看:如何设置组合框值,但我不确定它是否适用于我(可能是错的,请纠正我,如果我).我正在使用VB.Net,VS2012,我需要以编程方式设置数据块的数据块的值成员.
我的代码现在如下(这是从循环中分配一堆控件值):
cboCountry.SelectedValue = row.Item("CCCOUNTRY").ToString
Run Code Online (Sandbox Code Playgroud)
这不会分配任何选定的值.我也尝试过:
cboCountry.SelectedItem = cboCountry.FindString(row.Item("CCCOUNTRY").ToString)
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.对于这个例子:
同样,我需要做的就是以编程方式设置selectedvalue.任何帮助非常感谢!
selectedvalue ×10
combobox ×4
asp.net ×2
wpf ×2
.net ×1
ajax ×1
c# ×1
data-binding ×1
datatemplate ×1
gridview ×1
html ×1
html-select ×1
javascript ×1
radcombobox ×1
radgrid ×1
selenium ×1
telerik ×1
vb.net ×1
winforms ×1