以下是有什么区别的:
所有这些依赖项属性都在Selector类中定义.我经常混淆SelectedItem与SelectedValue,并SelectedValue用SelectedValuePath.
我想知道它们之间的区别,并且当我们使用它们,尤其是SelectedValue和SelectedValuePath.请用一些简单的例子解释它们的用法.
public class ComboboxItem {
public string Text { get; set; }
public string Value { get; set; }
public override string ToString() { return Text; }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = comboBox1.SelectedIndex;
int selecteVal = (int)comboBox1.SelectedValue;
ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
Run Code Online (Sandbox Code Playgroud)
我正在添加它们:
ComboboxItem item = new ComboboxItem();
item.Text = cd.Name;
item.Value = cd.ID;
this.comboBox1.Items.Add(item);
Run Code Online (Sandbox Code Playgroud)
我一直得到一个NullReferenceExeption,不知道为什么.文本似乎显示得很好.
我的WPF ComboBox仅包含文本条目.用户将选择一个.获取所选ComboBoxItem文本的最简单方法是什么?请在C#和Visual Basic中回答.这是我的ComboBox:
<ComboBox Name="cboPickOne">
<ComboBoxItem>This</ComboBoxItem>
<ComboBoxItem>should be</ComboBoxItem>
<ComboBoxItem>easier!</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我知道答案,但要找到它并不容易.我以为我会发布问题来帮助别人.修订:我学到了更好的答案.通过添加SelectedValuePath ="Content"作为ComboBox属性,我不再需要丑陋的强制转换代码.请参阅下面的安迪回答.
我有一个问题,其中所选值不适用于Html.DropDownList帮助器方法.见下文:
这是我的控制器:
public ActionResult Edit(int id = 0)
{
NewsEvent item = GetItem(id);
ViewBag.NewsItemId = new SelectList(ViewBag.NewsItemId.Items, "Id", "Name", item.NewsItemId);
return View(item);
}
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
@Html.DropDownList("NewsItemId",ViewBag.NewsItemId as SelectList, string.Empty,
new { @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)
但是,当我在视图中尝试以下时,它可以工作:
@Html.DropDownList("NewsItemId", string.Empty)
Run Code Online (Sandbox Code Playgroud)
以下也可以,但由于字段名称与模型不匹配,因此无法正确发布.
@Html.DropDownList("NewsItemIdDrop",ViewBag.NewsItemId as SelectList, string.Empty,
new { @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)
我需要使用第一个选项的原因是我可以将class属性添加到控件中.
有人能帮助我理解我做错了什么吗?
标记:
<div style="float:left;margin-top:15px;width:80px">
<asp:DropDownList ID="MyList" runat="server" Width="100px"></asp:DropDownList>
</div>
Run Code Online (Sandbox Code Playgroud)
码:
// clear vehicles list
MyList.Items.Clear();
// add 'all' option
MyList.Items.Add(new ListItem("ALL", "0"));
// add assets
foreach (CustomClass item in items)
MyList.Items.Add(new ListItem(item.Name, item.ID.ToString()));
Run Code Online (Sandbox Code Playgroud)
没有事件触发SelectedIndexChanged,因为没有必要.
当我单击回发按钮时,selecteditem的值仍然是DropDownList中第一个项目的值.我错过了什么?
注意 请停止回复和编辑帖子.我们可以保留它,因为它已经被回答了.
这段代码
<asp:DropDownList runat="server" ID="testdropdown" SelectedValue="2">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
产生此错误:
无法以声明方式设置"SelectedValue"属性.
然而,这是数据绑定GridViews的合法且常用的编辑模板.该SelectedValue属性肯定在这里以声明方式设置.
<EditItemTemplate>
<asp:DropDownList runat="server"
ID="GenreDropDownList"
DataSourceID="GenreDataSource"
DataValueField="GenreId"
DataTextField="Name"
SelectedValue='<%# Bind("Genre.GenreId") %>'>
</asp:DropDownList>
</EditItemTemplate>
Run Code Online (Sandbox Code Playgroud)
问题是:当你被允许以声明方式设置它和不允许你设置它的情况之间有什么区别?错误消息暗示它永远不会被允许.
asp.net data-binding selectedvalue templatefield drop-down-menu
在我的ASP.NET项目中.我有两个下拉列表和一个复选框.选中该复选框后,所选值DropDownList1必须与选中的值相同DropDownList2.但是DropDownList1.SelectedValue没有用.
这是我的代码:
protected void chkSameBAddress_CheckedChanged(object sender, EventArgs e)
{
try
{
if (this.chkSameBAddress.Checked == true)
{
this.txtcSAddress1.Text= this.txtcBAddress1.Text;
this.txtcSAddress2.Text = this.txtcBAddress2.Text;
this.txtcSAddress3.Text = this.txtcBAddress3.Text;
this.txtcSAddress4.Text = this.txtcBAddress4.Text;
this.txtcSCity.Text = this.txtcBCity.Text;
this.txtcSPostCode.Text = this.txtcBPostCode.Text;
this.txtcSState.Text = this.txtcBState.Text;
this.ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value).Selected = true;
}
}
catch (Exception ex)
{
logger.Error(ex.Message);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
如上例所示,如果选中chkSmaeBAddress,则所选的ddlcSCountry值必须与ddlcBCountry选定值相同.
在纯XAML中选择具有值(而不是索引)的组合框项目的正确语法是什么?
不起作用:
<StackPanel>
<ComboBox SelectedValue="CA">
<ComboBoxItem Tag="CO">Colorado</ComboBoxItem>
<ComboBoxItem Tag="CA">California</ComboBoxItem>
<ComboBoxItem Tag="NM">New Mexico</ComboBoxItem>
</ComboBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
不起作用:
<StackPanel>
<ComboBox SelectedValue="CA">
<ComboBoxItem Value="CO">Colorado</ComboBoxItem>
<ComboBoxItem Value="CA">California</ComboBoxItem>
<ComboBoxItem Value="NM">New Mexico</ComboBoxItem>
</ComboBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
即便这样也行不通:
<ComboBox SelectedValue="Colorado">
<ComboBoxItem Tag="CO">Colorado</ComboBoxItem>
<ComboBoxItem Tag="CA">California</ComboBoxItem>
<ComboBoxItem Tag="NM">New Mexico</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
这不起作用:
<StackPanel>
<ComboBox SelectedItem="CA">
<ComboBoxItem Tag="CO">Colorado</ComboBoxItem>
<ComboBoxItem Tag="CA">California</ComboBoxItem>
<ComboBoxItem Tag="NM">New Mexico</ComboBoxItem>
</ComboBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud) 我有一种感觉,我错过了一些非常明显的东西,我无法捕获DropDownList的选定值; 该值将重新启动列表中的第一个项目.我已将DropListList autopostback属性设置为true.我有一个粘贴在下面的SelectedIndexChangedEvent.这不在母版页上.
protected void ddlRestCity_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
r_city = ddlRestCity.SelectedValue.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
这是DropDownList控件:
<asp:DropDownList ID="ddlRestCity" runat="server"
Width="100px" AutoPostBack="True"
onselectedindexchanged="ddlRestCity_SelectedIndexChanged">
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
Thanx提前为您提供帮助!
我想将组合框中的值作为参数传递给SQL语句.Winforms组合框为我提供了几个检索值的选项,即SelectedItem,SelectedText和SelectedValue.在这种情况下哪一个最好/最安全?
selectedvalue ×10
asp.net ×4
c# ×4
combobox ×4
selecteditem ×4
wpf ×3
data-binding ×2
selectedtext ×2
xaml ×2
asp.net-mvc ×1
razor ×1
silverlight ×1
winforms ×1