简体   繁体   English

从另一个选择中更新下拉列表

[英]Update Drop Down List From Selection Of Another

I am needing to update the value of one drop down list based off the selection from a different drop down list. 我需要根据不同下拉列表中的选择来更新一个下拉列表的值。 The caveat that I have is instead of the drop down list containing the actual value displayed on the page, it is holding the id assigned, which skews my whole logic behind it! 我需要注意的是,它没有保存包含在页面上显示的实际值的下拉列表,而是保留分配的ID,这使我的整个逻辑错了! I am wanting to update ddlLocation based off the value selected for ddlCompName on the page itself ddlCompName holds a text value like On1, On2, On3, On4 but behind the scenes it is actually housing the value of CompID 我想基于在页面本身上为ddlCompName选择的值来更新ddlLocation ddlCompName拥有一个像On1, On2, On3, On4类的文本值On1, On2, On3, On4但在幕后它实际上是在容纳CompID的值

This is how ddlCompName gets populated 这就是ddlCompName的填充方式

private void GetComputerName()
{
  this.ddlCompName.DataSource = GetComputerNames(DBToQuery);
  this.ddlCompName.DataTextField = ComputerName;
  this.ddlCompName.DataValueField = CompID;
  this.ddlCompName.DataBind();
}

And this is the update I want to run, but doesn't work due to the numeric value 这是我要运行的更新,但是由于数值不起作用

protected void ddlCompName_SelectedIndexChanged(object sender, EventArgs e)
{
  if (VBS.Left(ddlCompName.SelectedValue.ToString(), 2) == "On")
  {
    ddlLocation.SelectedValue = "Building One";
  }
}

What do I need to alter so that I can make the update as desired? 我需要更改什么以便可以根据需要进行更新?

EDIT -- @Nelak altering to SelectedText instead of SelectedValue throws this compile error 编辑-@Nelak更改为SelectedText而不是SelectedValue引发此编译错误

'System.Web.UI.WebControls.DropDownList' does not contain a definition for 'SelectedText' and no extension method 'SelectedText' accepting a first argument of type 'System.Web.UI.WebControls.DropDownList' could be found (are you missing a using directive or an assembly reference?) 'System.Web.UI.WebControls.DropDownList'不包含'SelectedText'的定义,也找不到扩展方法'SelectedText'接受类型为'System.Web.UI.WebControls.DropDownList'的第一个参数(您是缺少using指令或程序集引用?)

If you want the display text rather than the value then try this 如果要显示文本而不是值,请尝试此操作

protected void ddlCompName_SelectedIndexChanged(object sender, EventArgs e)
{
  if (VBS.Left(ddlCompName.SelectedItem.Text, 2) == "On")
  {
    ddlLocation.SelectedValue = "Building One";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM