2015-10-16 77 views
0

我正在使用Asp .Net Web窗體。我有2個dropDown列表和1個輸入字段。我希望當我更改dropDown列表上的項目或字段值來調用某個事件,但它調用所有事件。DropDownList的SelectedIndexChanged事件調用所有事件

例如,如果我改變大小它調用Size_SelectedIndexChanged,然後Color_SelectedIndexChanged,然後txtKolicina_TextChanged 如果我改變顏色它調用Color_SelectedIndexChanged,比Size_SelectedIndexChanged然後txtKolicina_TextChanged。

任何幫助?

<asp:DropDownList ID="Colors" runat="server" AutoPostBack="true" CssClass="form-control detal-page-input" Style="height: 30px;" OnSelectedIndexChanged="Colors_SelectedIndexChanged" AppendDataBoundItems="True" DataSourceID="LinqDataSource3" DataTextField="color" DataValueField="color" > 
               <asp:ListItem Text="" Value=""></asp:ListItem> 
              </asp:DropDownList> 

<asp:DropDownList ID="Size" runat="server" OnSelectedIndexChanged="Size_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true" CssClass="form-control detal-page-input" Style="height: 30px;" DataSourceID="LinqDataSource2" DataTextField="size" DataValueField="size" EnableViewState="true"> 
               <asp:ListItem Text="" Value=""></asp:ListItem> 
              </asp:DropDownList> 


    <asp:TextBox Name="txtKolicina" ID="txtKolicina" runat="server" CssClass="form-control form-numberone detal-page-input" OnTextChanged="txtKolicina_TextChanged" ></asp:TextBox> 

這是後端

protected void Size_SelectedIndexChanged(object sender, EventArgs e) 
    { 
//do something 
} 
    protected void Colors_SelectedIndexChanged(object sender, EventArgs e) 
    { 
//do something 
} 
protected void txtKolicina_TextChanged(object sender, EventArgs e) 
    { 
//do something 
} 

UPDATE

public string[] GetColor() 
{ 
    CMS_Shop_ModuleDataContext db = new CMS_Shop_ModuleDataContext(); 
    var color = (from p in db.CMS_Articles 
     where 
      p.articleID == int.Parse(HiddenFieldArticalId.Value) || 
      p.sameAsArticleID == int.Parse(HiddenFieldArticalId.Value) 
     //where p.articleID == 10049 || p.sameAsArticleID == 10049 
     select p.color).Distinct(); 
    return color.ToArray(); 
} 
public int GetColorCount() 
{ 
    CMS_Shop_ModuleDataContext db = new CMS_Shop_ModuleDataContext(); 
    var color = (from p in db.CMS_Articles 
     where (p.articleID == int.Parse(HiddenFieldArticalId.Value) 
       || p.sameAsArticleID == int.Parse(HiddenFieldArticalId.Value)) 
       && p.color != "" 
     select p.color); 

    return color.Distinct().Count(); 

} 
protected void Page_Load(object sender, EventArgs e) 
{ 

    if (!Page.IsPostBack) 
    { 
if (GetColorCount() == 0) 
     { 

      Colors.Visible = false; 
      lblBoja.Visible = false; 

     } 
     else 
     { 
      Colors.Visible = true; 
      lblBoja.Visible = true; 



     } 

回答

0

好吧,我發現是什麼問題。 變化

的EnableViewState =「假」

EnableViewState="true" 
0

看起來你使用視圖狀態和代碼重新裝載您DropDownLists後面的回發。你能顯示OnInit和OnLoad嗎?

或者只是試試這個代碼:

if (!IsPostBack) { 
    // populate all drop downs lists 
} 
+0

對不起,遲到的回答你能告訴在Page_Load其餘部分我更新了我的代碼 – usesser

+0

? if(!Page.IsPostBack)的else塊是什麼? – MinM

相關問題