2011-05-26 45 views
0

當我從此下拉列表(DDL1)中選擇任何項目時導致創建另一個下拉列表(DDL2),其中包含一些下拉列表(DDL1)項目。當我從DDL1中選擇其他項目時,項目將在DDL2中更改,這發生在DDL1中選擇的每個不同項目。從動態添加的下拉列表中選擇項目時看不到標籤內容

當我從DDL2中選擇一個項目時,必須顯示標籤內容,我正在製作Label invisibe,並在代碼中將可見性更改爲true並向其添加內容。但是當我從DDL2中選擇一個項目時,標籤內容不顯示。

這裏是我的代碼

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedValue == "Abe Books") 
    { 
     DropDownSeller.Visible = true; 
     lnkUsdBooks.Visible = true; 
     lnkUsdBooks.Text = "[email protected]"; 
     lnkUsdBooks.NavigateUrl = "mailto:[email protected]"; 
     DropDownSeller.Visible = true; 
     DropDownSeller.Items.Remove("Chacha Choudary"); 
     DropDownSeller.Items.Remove("SpiderMan"); 
     DropDownSeller.Items.Remove("Amar chitra Katha"); 
     DropDownSeller.Items.Remove("Chandamama"); 
     DropDownSeller.Items.Remove("Mahabharata"); 
     DropDownSeller.Items.Add("Amar chitra Katha"); 
     DropDownSeller.Items.Add("Chandamama"); 
     DropDownSeller.Items.Add("Mahabharata"); 
     DropDownSeller.DataBind(); 

      if (DropDownSeller.SelectedValue == "Amar chitra Katha") 
      { 
       lblPrice.Visible = true; 
       lblPrice.Text = "$69.99"; 
      } 
      else if (DropDownSeller.SelectedValue == "Chandamama") 
      { 
       lblPrice.Visible = true; 
       lblPrice.Text = "$59.99"; 
      } 
      else if (DropDownSeller.SelectedValue == "Mahabharata") 
      { 
       lblPrice.Visible = true; 
       lblPrice.Text = "$49.99"; 
      } 
      else 
      { 
       lblPrice.Visible = false; 
      } 
     } 

任何想法在這是值得讚賞

感謝,

+0

「但是,當我選擇一個DDL2項目不顯示的標籤內容」。那個事件的代碼在哪裏? – Dante 2011-05-26 13:31:23

+0

您正在將項目添加到'DropDownSeller'並檢查您剛剛添加的項目是否也被選中了? – 2011-05-26 13:33:27

+0

@Bala R我已經設置了AutoPostBack值爲true – user490706 2011-05-26 13:43:21

回答

1

DropDownList1_SelectedIndexChanged刪除if (!Page.IsPostBack)因爲當頁面回傳此條件將是錯誤的。因爲你的頁面發回服務器,這就是爲什麼它不可見和不顯示。

總之你DropDownList1_SelectedIndexChanged應該是這樣..

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedValue == "Abe Books") 
    { 
     DropDownSeller.Visible = true; 
     lnkUsdBooks.Visible = true; 
     lnkUsdBooks.Text = "[email protected]"; 
     lnkUsdBooks.NavigateUrl = "mailto:[email protected]"; 
     DropDownSeller.Visible = true; 

     DropDownSeller.Items.Clear(); // it will clear all the items, instead you are removing one by one 

     DropDownSeller.Items.Add("Amar chitra Katha"); 
     DropDownSeller.Items.Add("Chandamama"); 
     DropDownSeller.Items.Add("Mahabharata"); 
     DropDownSeller.DataBind(); 
    } 
    protected void DropDownSeller_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (DropDownSeller.SelectedValue == "Amar chitra Katha") 
     { 
      lblPrice.Visible = true; 
      lblPrice.Text = "$69.99"; 
     } 
     else if (DropDownSeller.SelectedValue == "Chandamama") 
     { 
      lblPrice.Visible = true; 
      lblPrice.Text = "$59.99"; 
     } 
     else if (DropDownSeller.SelectedValue == "Mahabharata") 
     { 
      lblPrice.Visible = true; 
      lblPrice.Text = "$49.99"; 
     } 
     else 
     { 
      lblPrice.Visible = false; 
     } 
    } 
+0

就像我正要寫的一樣。如果你通過你的代碼,你將能夠看到這一點。 – anothershrubery 2011-05-26 13:32:29

+0

我刪除了(!page.IsPostback),當我從DDL2中選擇項目時仍然看不到標籤內容 – user490706 2011-05-26 13:41:40

+0

我已編輯我的答案。現在試試。你必須使用DropDownSeller選擇索引更改,因爲你想在DropDownSeller上可見 – 2011-05-26 13:50:02

相關問題