2016-10-26 197 views
-1
    <div class="form-group col-lg-12"> 
         <label for="ddl1">1:</label> 
         <asp:DropDownList ID="ddl1" name="ddl1" CssClass="form-control" runat="server" /> 
        </div> 

        <div class="form-group col-lg-12"> 
         <label for="ddl2">1:</label> 
         <asp:DropDownList ID="ddl2" name="ddl2" CssClass="form-control" runat="server" required /> 
        </div> 

我試圖根據ddl1的值選擇ddl2的下拉列表項目。因此,「if」函數的Get2爲& Get2。 注意:每個ddl的數據將來自我的數據庫中的表格。根據第一個選擇的下拉列表項加載第二個下拉列表項目

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     ddl1Get(); 
     Get1(); 
     Get2(); 
    } 

    #region "Function" 
    if (ddl1.SelectedItem.Value == "1") 
    { 
     Get1(); 
    } 
    else if (1.SelectedItem.Value == "2") 
    { 
     Get2(); 
    } 
    #endregion 
} 

    #region "Function" 
    if (ddl1.SelectedItem.Value == "1") 
    { 
     Get1(); 
    } 
    else if (ddl1.SelectedItem.Value == "2") 
    { 
     Get2(); 
    } 
#endregion 

void Get1() 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = con; 
    cmd.CommandText = "SELECT ID, Category FROM Table1"; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    ddl2.DataSource = dr; 
    ddl2.DataTextField = "Category"; 
    ddl2.DataValueField = "Category"; 
    ddl2.DataBind(); 
    con.Close(); 

    ddl2.Items.Insert(0, new ListItem(" ", "")); 
} 

void Get2() 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = con; 
    cmd.CommandText = "SELECT ID, Category FROM Table2"; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    ddl2.DataSource = dr; 
    ddl2.DataTextField = "Category"; 
    ddl2.DataValueField = "Category"; 
    ddl2.DataBind(); 
    con.Close(); 

    ddl2.Items.Insert(0, new ListItem(" ", "")); 
} 
void ddlGet() 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = con; 
    cmd.CommandText = "SELECT ID, Category FROM Table0"; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    ddl1.DataSource = dr; 
    ddl1.DataTextField = "Category"; 
    ddl1.DataValueField = "Category"; 
    ddl1.DataBind(); 
    con.Close(); 

    ddl1.Items.Insert(0, new ListItem(" ", "")); 
} 

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (ddl1.SelectedItem.Value == "1") 
    { 
     Get1(); 
    } 
    else if (ddl1.SelectedItem.Value == "2") 
    { 
     Get2(); 
    } 
} 
+0

你的Page_Load事件是什麼樣子..這是你應該檢查IsPostBack等事情的地方。你甚至試圖通過使用調試器的代碼..?我也會認爲你在Select查詢中的第二個下拉列表,你會看到一個Where子句... – MethodMan

+0

也是爲什麼你向我們展示'void Get2()'方法兩次是你粘貼代碼中的一個錯誤。 。? – MethodMan

+0

@MethodMan我不好,我現在修好了。 – Allocen

回答

0

使用OnSelectedIndexChanged事件asp:DropDownList

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (ddl1.SelectedItem.Value == "1") 
    { 
    Get1(); 
    } 
    else if (ddl1.SelectedItem.Value == "2") 
    { 
    Get2(); 
    } 
} 

另外:

ddl1.DataTextField = "Category"; 
    ddl1.DataValueField = "ID"; 
2

還有就是對的SelectedIndexChanged在下拉dropdown..just雙擊並複製裏面的代碼。那麼它應該工作得很好。

public void ddl1_selectedIndexChanged(sender,e){ //your code here } 

另外你在代碼中提到了兩次Get2()。這是一個複製錯誤還是你沒有注意到你的代碼?這可能是問題,即使選定的值發生變化,爲什麼你得到相同的值綁定

+0

我的不好,我現在糾正了它。但仍然不起作用。還編輯了代碼。添加頁面載入和indexchanged。 – Allocen

+0

爲ddl1設置AutoPostBack =「true」並嘗試 – James

相關問題