2013-07-22 73 views
0

這裏我試圖根據從另一個下拉列表中選擇的值填充下拉列表。這裏是我的兩個代碼:dropdown_SelectedIndexChanged僅適用於第二次

<div> 
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
    </asp:DropDownList> 

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"> 
    </asp:DropDownList> 
</div> 
</form> 

這裏是後臺代碼:

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


    public void getProjectId() 
     { 
      string projName, projId, projFinal; 
      using (var cmdScope_ID = new SqlCommand("SELECT [ProjectId],[ProjectName] FROM [DB].[dbo].[Sample Table]", con)) 
      { 
       using (var reader = cmdScope_ID.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         projId = reader[0].ToString(); 
         projName = reader[1].ToString(); 
         projFinal = projId + "-" + "[" + projName + "]"; 
         DropDownList1.Items.Insert(0, new ListItem(projFinal, "1")); 
        } 
       } 
      } 
     } 

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //code to get data from DB and populate DropDownList2 
} 

我的問題是,當我從第一個下拉列表中的任何條目,什麼都不會發生在第一時間。只有頁面重新加載。但是如果我再次執行此操作,則只會填充第二個下拉列表。

此外,如果我保持if (!IsPostBack)的條件,什麼都不會發生。但是,如果我刪除它,那麼第一個下拉列表會填充兩次,然後只有第二個下拉列表出現。 有什麼建議爲什麼會發生這種情況?

編輯:我已經添加了代碼來填充第一個下拉列表。第二個下拉列表也是相同的代碼。

+0

您是否嘗試調試並查看PageLoad上發生了什麼以及所選索引更改事件? –

+0

是的...如果我不使用'!IspostBack',編譯器通過getDeveloperId()重新執行一次。但是,如果我再次更改下拉菜單,那麼只有它來到selectedindexchanged方法 – nitinvertigo

+0

,但如果我使用Ispostback,它只是填充第一個下拉菜單,然後沒有任何反應 – nitinvertigo

回答

1

嘿問題是,線DropDownList1.Items.Insert(0, new ListItem(projFinal, "1"));

因爲你在index "0"將項目與價值1和編譯器被不附selectedchange事件下拉列表中。

請將不同索引上的項目與值綁定,然後只解決您的問題。

我已經做了一個簡單的程序來測試你的問題。敬請期待。

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

    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     List<int> lst = 
     new List<int>(); 
     lst.Add(12); 
     lst.Add(22); 
     lst.Add(32); 
     DropDownList2.DataSource = lst; 
     DropDownList2.DataBind(); 
    } 

    private void fill() 
    { 
     List<int> lst = 
     new List<int>(); 
     lst.Add(1); 
     lst.Add(2); 
     lst.Add(3); 
     var count=0; 
     foreach (var i in lst) 
     { 
      DropDownList1.Items.Insert(count, new ListItem(i.ToString(), count.ToString())); 
      count++; 
     } 


    } 

希望它會幫助你...

1

我已經改變了數據綁定到下拉的方式。現在它工作正常。

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       getDeveloperId(); 
      } 
     } 
     public void getDeveloperId() 
     { 
      con.Open(); 
      SqlDataAdapter da = new SqlDataAdapter("SELECT [DeveloperId], [DeveloperName] FROM [DB].[dbo].[tbl]", con); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      DropDownList1.DataSource = ds.Tables[0]; //bind the ddp_dataview to dataview 
      DropDownList1.DataTextField = "DeveloperName"; 
      DropDownList1.DataValueField = "DeveloperId"; 
      DropDownList1.DataBind(); 
     } 
相關問題