2016-09-27 79 views
0

當天的問候!如果第一個組合框的值在C#中,則啓用第二個組合框#

我是DOT NET的新手。開發一個工具,建立用戶界面。

我被困在生成其他組合框的選擇組合框值。

的問題是: 有2個組合框:DropDownList1和DropDownList2 DropDownList1值:0 - 所有1 - 不計劃2 - 已拒絕3選擇 DropDownList2值:0所有選擇1 - 溝通不暢3 - 無法在測試4差打字技巧

值在組合框中填充。

//爲DropDownList1

  SqlConnection con = new SqlConnection("Data Source=.;Initial     Catalog=Test_DB;User ID=sa"); 
     con.Open(); 

     //Interview Status 
     string Sql = "select Id,Status from dbo.InterviewStatus"; 

     SqlCommand cmd = new SqlCommand(Sql, con); 
     cmd.CommandType = CommandType.Text; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      DropDownList1.DataSource = ds.Tables[0]; 
      DropDownList1.DataTextField = "Status"; 
      DropDownList1.DataValueField = "Id"; 
      DropDownList1.DataBind(); 
      DropDownList1.Items.Insert(0, new ListItem("All", "0")); 
     } 

現在,DropDownList2應該只有當DropDownList1被拒絕啓用。 對於其他DropDownList1值,應該禁用DropDownList2。

請幫我解決這個問題。

感謝提前:)

+0

請看看[如何對提問](http://stackoverflow.com/help/how-to-ask)。你嘗試了什麼,你會得到哪些錯誤信息? – swe

+0

您的發佈代碼中沒有明顯提及任何'ComboBox'。這對解決您的問題沒有多大幫助。請按照@swe –

+0

@swe的建議 - 謝謝.. !!更新 – user3497375

回答

0

你可以做級聯樣式按照下面給出的方法下拉列表,你也可以在填充cboComments DropDownList初始化過程本身作爲數據很小,因爲它是一個aspx控件,您通常會在Page_Load事件中執行它,您可以看到this線程以獲取更多信息。

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CascadingDDL._Default" %> 

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 

    <div class="jumbotron"> 
     <h1>Cascading Drop Down List</h1> 
    </div> 

    <div class="row"> 
     <div class="col-md-12"> 
      <asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboStatus_SelectedIndexChanged"> 
       <asp:ListItem Value="0" Text="All"></asp:ListItem> 
       <asp:ListItem Value="1" Text="Not Scheduled"></asp:ListItem> 
       <asp:ListItem Value="2" Text="Rejected"></asp:ListItem> 
       <asp:ListItem Value="3" Text="Selected"></asp:ListItem> 
      </asp:DropDownList> 

      <asp:DropDownList ID="cboComments" runat="server"> 
       <asp:ListItem Value="0" Text="Select All"></asp:ListItem> 
       <asp:ListItem Value="1" Text="Poor Communication"></asp:ListItem> 
       <asp:ListItem Value="3" Text="Failed in Test"></asp:ListItem> 
       <asp:ListItem Value="4" Text="Poor Typing skill"></asp:ListItem> 
      </asp:DropDownList> 
     </div> 
    </div> 

</asp:Content> 

代碼隱藏

namespace CascadingDDL 
{ 
    using System; 
    using System.Web.UI; 

    public partial class _Default : Page 
    { 
     public const int StatusRejected = 2; 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!this.IsPostBack) 
      { 
       this.cboComments.Enabled = false; 
      } 
     } 

     protected void cboStatus_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      var status = int.Parse(this.cboStatus.SelectedValue); 
      if (status == StatusRejected) 
      { 
       this.cboComments.Enabled = true; 
      } 
      else 
      { 
       this.cboComments.Enabled = false; 
      } 
     } 
    } 
} 
+0

非常感謝!謝謝 !! – user3497375

0

如果我理解正確的話,你可以使用SelectedIndexChanged事件,並檢查您cboStatus

private void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedItem.ToString() == "2") 
    { 
     DropDownList2.Enabled = true; 
    } 
} 

編輯值:

由於指數似乎與您實際只需要索引的Status值一致,除非它是用戶的重要信息。要填充你ComboBox從您的數據庫剛剛得到的值作爲List和使用AddRange方法

DropDownList1.Items.AddRange(listOfValues.ToArray()); 
+0

謝謝..!但是,在哪裏填充cboComments值..我正在使用SQL來生成組合框值 – user3497375

+0

這實際上是一個不同的問題。查看我的編輯 –

相關問題