2016-12-02 82 views
0

如果您协助我,这将非常棒。在第三个下拉列表中比较两个下拉列表值和更改项目

我有三个dropdownlists。

<asp:DropDownList ID="DropDownList1" class="form-control input-sm" runat="server" Width="147px"> 
    <asp:ListItem>2</asp:ListItem> 
    <asp:ListItem>4</asp:ListItem> 
    <asp:ListItem>5</asp:ListItem> 
    <asp:ListItem>6</asp:ListItem> 

    <asp:ListItem>8</asp:ListItem> 
    <asp:ListItem>10</asp:ListItem> 
    <asp:ListItem>48</asp:ListItem> 
    <asp:ListItem>60</asp:ListItem> 
</asp:DropDownList> 

<asp:DropDownList ID="DropDownList2" class="form-control input-sm" runat="server" Width="147px"> 
    <asp:ListItem>0.5</asp:ListItem> 
    <asp:ListItem>1.0</asp:ListItem> 
    <asp:ListItem>1.5</asp:ListItem> 
    <asp:ListItem>2</asp:ListItem> 
    <asp:ListItem>2.5</asp:ListItem> 
    <asp:ListItem>3</asp:ListItem> 
    <asp:ListItem>3.5</asp:ListItem> 
    <asp:ListItem>4</asp:ListItem> 
    <asp:ListItem>4.5</asp:ListItem> 
    <asp:ListItem>8.5</asp:ListItem> 
    <asp:ListItem>9</asp:ListItem> 
    <asp:ListItem>10</asp:ListItem> 
    <asp:ListItem>12</asp:ListItem> 
    <asp:ListItem>24</asp:ListItem> 
    <asp:ListItem>48</asp:ListItem> 
</asp:DropDownList> 

<asp:DropDownList ID="DropDownList3" class="form-control input-sm" runat="server" Width="147px"> 
    <asp:ListItem>MET</asp:ListItem> 
    <asp:ListItem>Threatened</asp:ListItem> 
    <asp:ListItem>Breached</asp:ListItem> 
</asp:DropDownList> 

现在我的问题是:

如果下拉列表值1> dropdownlist2值,那么,我应该只看到dropdownlist3蛋氨酸项目(其余两个项目应该隐藏。)

再次,

如果dropdownlist 1的值为< dropdownlist2值,那么我只能在dropdownlist3中看到Breached项目(其余两项应隐藏)。

你能帮助这个代码?

+0

所以你问....它的逻辑? – SANDEEP

+0

谷歌“SelectedIndexChanged下拉事件” –

+0

我想你想要一个简单的jQuery函数来隐藏/显示使用hide()取决于下拉列表的值。 – PurpleSmurph

回答

0

OnSelectedIndexChanged事件添加到DropDownList1DropDownList2并设置AutoPostBacktrue

<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" AutoPostBack="true"> 
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" AutoPostBack="true"> 

然后在后面

protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //get the values from the dropdownlists 
    decimal selectedValue1 = Convert.ToDecimal(DropDownList1.SelectedValue); 
    decimal selectedValue2 = Convert.ToDecimal(DropDownList2.SelectedValue); 

    //clear all listitems from dropdownlist3 
    DropDownList3.Items.Clear(); 

    //add the listitems based on the selected values 
    if (selectedValue1 > selectedValue2) 
    { 
     DropDownList3.Items.Insert(0, new ListItem("MET", "MET", true)); 
    } 
    else if (selectedValue1 < selectedValue2) 
    { 
     DropDownList3.Items.Insert(0, new ListItem("Breached", "Breached", true)); 
    } 
    else 
    { 
     DropDownList3.Items.Insert(0, new ListItem("Threatened", "Threatened", true)); 
    } 
} 
0

代码,让您的前端一些变化

添加AutoPostBack="True"在均为DropDownList1 & DropDownList2

现在,在后端你的Page_Load

protected void Page_Load(object sender, EventArgs e) 
    { 
     if(IsPostBack) 
     { 
      int a = Convert.ToInt32(DropDownList1.SelectedItem.Text); 
      double b = Convert.ToDouble(DropDownList2.SelectedItem.Text); 
      DropDownList3.Items.Clear(); 
      if (a > b) 
      { 
       DropDownList3.Items.Insert(0, "MET"); 
      } 
      else if (a < b) 
      { 
       DropDownList3.Items.Insert(0, "Breached"); 
      } 
      else 
      { 
       DropDownList3.Items.Insert(0, "MET"); 
       DropDownList3.Items.Insert(1, "Breached"); 
       DropDownList3.Items.Insert(2, "Threatened"); 
      } 
     } 
    } 
相关问题