所以我有两个这样的多选框
<select id="foo" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<select id="bar" multiple="multiple">
<option value="1">Opt 1</option>
<option value="2">Opt 2</option>
<option value="3">Opt 3</option>
<option value="4">Opt 4</option>
</select>
<a href="#" onclick="select()">Select</a>
Run Code Online (Sandbox Code Playgroud)
我要做的是,当点击"选择"时,将选择"#bar"中与"#foo"中的选项具有相同值的任何选项.在这种情况下,应选择"#bar"中的选项1和选项2.我不知道为什么我的javascript无法正常工作.我知道这一定很简单.我只是看不到它.:(所以我的Javascript函数如下:
function select(){
var vals = new Array();
var iter = 0;
$("#foo option").each(function(){
var v = $(this).val();
$('#bar option').each(function(){
if ($(this).val() == v)
{
vals[iter] = v;
iter++;
break;
}
});
});
$("#bar").val(vals);
}
Run Code Online (Sandbox Code Playgroud) ComboBox我的WPF应用程序中有一个.使用下面的代码我可以设置ToolTip为选择的值:
ToolTip="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}"
Run Code Online (Sandbox Code Playgroud)
但是,如果我需要ToolTip根据ComboBox选择设置单独的值,则以下代码不起作用:
<controls:ComboBoxEx.Style>
<Style TargetType="ComboBox" BasedOn="{StaticResource basicStyle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}" Value="DAW">
<Setter Property="ToolTip" Value="abc"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}" Value="generic">
<Setter Property="ToolTip" Value="def"/>
</DataTrigger>
</Style.Triggers>
</Style>
</controls:ComboBoxEx.Style>
Run Code Online (Sandbox Code Playgroud) 如何在转发器中设置dropDownList的选定项?
转发器绑定到repeaterData DataTable,dropDownList绑定到后面的代码中的dropDownList DataTable.我需要将DropDownList的SelectedValue属性设置为repeaterData表中的字段值.
这就是我尝试过的:
<asp:Repeater runat="server" ID="myRepeater>
<ItemTemplate>
<asp:DropDownList runat="server" CssClass="fullSelect" ID="degree_dropdown"
AppendDataBoundItems="true"
selectedValue='<%#DataBinder.Eval(Container.DataItem,"degreeCode")%>'>
<asp:ListItem Text="Select Degree" />
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)
填充转发器的代码:
myRepeater.DataSource = myRepeaterData; //myRepeaterData is a datatable
myRepeater.DataBind();
Run Code Online (Sandbox Code Playgroud)
用于填充下拉列表的代码:
protected void educationPopup_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList degree_dropdown = e.Item.FindControl("degree_dropdown") as DropDownList;
if (degree_dropdown != null)
{
degree_dropdown.DataSource = degrees; //a datatable
degree_dropdown.DataTextField = "degree";
degree_dropdown.DataValueField = "code";
degree_dropdown.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud) 我是Grails framwork的新手.我有问题:
我有域名是:"国家".我想通过在Grails中使用g:select来在select标签中显示列表国家/地区
1英格兰
2加拿大
3德国
现在,我希望设置"加拿大"被选中.谁能帮我.请!
我的模型如下:
public class testCreateModel
{
public string s1 { get; set; }
public SelectList DL { get; set; }
public testCreateModel()
{
Dictionary<string, string> items = new Dictionary<string, string>();
items.Add("1", "Item 1");
items.Add("2", "Item 2");
DL = new SelectList(items, "Key", "Value");
}
}
Run Code Online (Sandbox Code Playgroud)
我的启动行动是:
public ActionResult testCreate()
{
testCreateModel model = new testCreateModel();
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
我的剃刀视图(删除了相关部分)是:
@model Tasks.Models.testCreateModel
@using (Html.BeginForm()) {
<fieldset>
<legend>testCreateModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.s1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.s1)
</div>
<div class="editor-label">
Select an …Run Code Online (Sandbox Code Playgroud) 我最近在ASP.NET DropDownList中发现了一个奇怪的行为,我希望有人可以解释一下.
基本上我遇到的问题是在回发之前进行数据绑定,然后将数据项设置为数据项SelectedValue列表中不存在的值时,调用根本没有效果.但是在回发时,同一个呼叫将失败ArgumentOutOfRangeException()
'cmbCountry'有一个SelectedValue,它是无效的,因为它在项目列表中不存在.参数名称:value
我正在使用以下代码.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cmbCountry.DataSource = GetCountries();
cmbCountry.DataBind();
cmbCountry.SelectedValue = ""; //No effect
}
else
{
cmbCountry.SelectedValue = ""; //ArgumentOutOfRangeException is thrown
}
}
protected List<Country> GetCountries()
{
List<Country> result = new List<Country>();
result.Add(new Country() { ID = Guid.NewGuid(), Description = "Test" });
result.Add(new Country() { ID = Guid.NewGuid(), Description = "Test1" });
result.Add(new Country() { ID = Guid.NewGuid(), Description = "Test2" });
result.Add(new Country() { …Run Code Online (Sandbox Code Playgroud) 我有以下数据模板(以及相应的视图模型,未显示):
<DataTemplate DataType="{x:Type logic:SnapshotListViewModel}">
<ListBox ItemsSource="{Binding Snapshots}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
ItemsSource绑定到viewmodel中的Snapshot列表.我的目标是清除SelectedItem,因此列表框将返回其初始的未选定状态.视图模型实现IPropertyNotified.
我在XAML中添加了一个绑定,如下所示:
<ListBox SelectedItem={Binding SelectedSnapshot} .... />
Run Code Online (Sandbox Code Playgroud)
在视图模型中,我设置SelectedSnapshot = null,但没有任何反应,即使在属性上调用了RaisePropertyChanged.
我再次尝试使用SelectedIndex而不是SelectedItem.仍然没有运气.
我终于找到了解决方案,我将在下面详述.
我有一个asp.net dropDownList,它自动绑定到sqlDataSource到页面加载时客户端类型的值.在页面加载时,我也在创建一个Client对象,其中一个属性是ClientType.我试图根据Client对象的ClientType属性的值设置ddl的SelectedValue失败.我收到以下错误消息"System.ArgumentOutOfRangeException:'ddlClientType'具有一个无效的SelectedValue,因为它不存在于项列表中".我知道这是因为当我尝试设置所选值时,列表尚未填充.有没有办法克服这个问题?谢谢!
好吧,经过几个小时阅读这里的东西,尝试所有的解决方案失败,也发现这篇文章,我认为这将拯救我的生命......没有.
长话短说.
这是我的观点(所有组合)
@Html.DropDownList("yearDropDown",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDownxxx",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown",(<SelectList>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown")
Run Code Online (Sandbox Code Playgroud)
这是我的控制器
public ActionResult(int year)
{
var years = new int[] { 2007, 2008, 2009, 2010, 2011, 2012 }
.Select(x => new SelectListItem {
Text = x.ToString(),
Value = x.ToString(),
Selected=x==year }).Distinct().ToList();
years.Insert(0, new SelectListItem { Value = null, Text = "ALL YEARS" });
ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());
return View();
}
Run Code Online (Sandbox Code Playgroud)
这是我呈现的HTML.选择无处可寻.
<select id="yearDropDown" name="yearDropDown"><option value="">ALL YEARS</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option> …Run Code Online (Sandbox Code Playgroud) 我确实在数据绑定和 MVVM 方法论上挣扎,尽管我喜欢这个概念,但我只是在挣扎。我创建了一个 WPF,它有多个组合框和一个按钮。第一个组合框将列出数据库实例名称。单击按钮后将填充剩余的组合框。由于我对第一个数据库实例组合框有问题,我将只显示我的代码。当应用程序启动时,将加载组合框并按预期选择第一项。问题是,当我选择一个新名称时,我希望调用的方法却没有选择。有人可以帮助我理解为什么public DBInstance SelectedDBInstance当我的 XAML 中有此方法时我的方法没有被执行吗SelectedValue="{Binding SelectedDBInstance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}?
这是我的数据库实例组合框的 XAML。我在这里遇到的一个问题是“值”fpr SelectedValuePath,如果我将其更改为“DBInstanceName”,它将不起作用。
<ComboBox x:Name="cbxRLFDBInstances" ItemsSource="{Binding DBInstances}"
SelectedValue="{Binding SelectedDBInstance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="value" DisplayMemberPath="DBInstanceName"/>
Run Code Online (Sandbox Code Playgroud)
这是我的视图模型代码:
namespace DatabaseTest.ViewModel
{
class RLFDatabaseTableViewModel : INotifyPropertyChanged
{
Utilities dbtUtilities = new Utilities();
public RelayCommand LoadDBInfoCommand
{
get;
set;
}
public RLFDatabaseTableViewModel()
{
LoadDBInstances();
LoadDBInfoCommand = new RelayCommand(LoadDBInfo);
}
public ObservableCollection<DBInstance> DBInstances
{
get;
set;
}
public void LoadDBInstances()
{
ObservableCollection<DBInstance> dbInstances = new ObservableCollection<DBInstance>();
DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
dbInstances.Add(new DBInstance …Run Code Online (Sandbox Code Playgroud) selectedvalue ×10
c# ×4
asp.net ×3
wpf ×3
html-select ×2
mvvm ×2
razor ×2
asp.net-3.5 ×1
combobox ×1
data-binding ×1
databound ×1
grails ×1
html ×1
javascript ×1
jquery ×1
null ×1
postback ×1
repeater ×1
selected ×1
selecteditem ×1
tooltip ×1
triggers ×1