2010-06-25 132 views
4

我想給一個表單中的多個DropDownLists分配相同的數據源,當頁面加載時,只有第一個下拉列表填充了數據源的內容,其他的是空的,缺少的是什麼?感謝您的回答。這裏是代碼;將同一個數據源分配給多個DropDownLists?

<form id="form1" runat="server">  
    <asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager> 
<asp:UpdatePanel ID="panel1" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
    <div id="div1" align="center"> 
     <table> 
     <tr> 
     <td><b>Brand-Model</b></td> 
     <td><asp:TextBox ID="brandText" runat="server" BorderStyle="Inset"></asp:TextBox></td> 
     <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="brandText" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator></td> 
     </tr> 
     <tr> 
     <td><b>Black</b></td> 
     <td><asp:DropDownList ID="blackList" runat="server"></asp:DropDownList></td> 
     <td><asp:HyperLink ID="HyperLink1" runat="server" Text="Add Cartridge" NavigateUrl="~/Admin/addCartridge.aspx"></asp:HyperLink></td> 
     </tr> 
     <tr> 
     <td><b>Color1</b></td> 
     <td><asp:DropDownList ID="colorList1" runat="server"></asp:DropDownList></td> 
     </tr> 
     <tr> 
     <td><b>Color2</b></td> 
     <td><asp:DropDownList ID="colorList2" runat="server"></asp:DropDownList></td> 
     </tr> 
     <tr> 
     <td><b>Color3</b></td> 
     <td><asp:DropDownList ID="colorList3" runat="server"></asp:DropDownList></td> 
     </tr> 
     <tr> 
     <td><b>Other1</b></td> 
     <td><asp:DropDownList ID="otherColor1" runat="server"></asp:DropDownList></td> 
     </tr> 
     <tr> 
     <td><b>Other2</b></td> 
     <td><asp:DropDownList ID="otherColor2" runat="server"></asp:DropDownList></td> 
     </tr> 
     <tr> 
     <td><b>Other3</b></td> 
     <td><asp:DropDownList ID="otherColor3" runat="server"></asp:DropDownList></td> 
     </tr> 
     <tr> 
     <td></td> 
     <td><asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_OnClick" /></td>  
     </tr>    
     <tr> 
     <td></td> 
     <td><asp:Label ID="submitInfo" runat="server"></asp:Label></td>  
     </tr> 
     </table> 
    </div> 
    </ContentTemplate> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="submit" EventName="Click" /> 
    </Triggers> 
    </asp:UpdatePanel> 
</form> 

protected void FillTheDropDownLists() 
    { 
     SqlApplication con = new SqlApplication(); 
     try 
     { 
      SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection()); 
      con.OpenSqlConnection(); 

      SqlDataReader reader = cmd.ExecuteReader(); 

      blackList.DataValueField = "name"; 
      blackList.DataSource = reader; 
      blackList.DataBind(); 

      colorList1.DataValueField = "name"; 
      colorList1.DataSource = reader; 
      colorList1.DataBind(); 

      colorList2.DataValueField = "name"; 
      colorList2.DataSource = reader; 
      colorList2.DataBind(); 

      colorList3.DataValueField = "name"; 
      colorList3.DataSource = reader; 
      colorList3.DataBind(); 

      otherColor1.DataValueField = "name"; 
      otherColor1.DataSource = reader; 
      otherColor1.DataBind(); 

      otherColor2.DataValueField = "name"; 
      otherColor2.DataSource = reader; 
      otherColor2.DataBind(); 

      otherColor3.DataValueField = "name"; 
      otherColor3.DataSource = reader; 
      otherColor3.DataBind(); 

      reader.Close(); 
     } 
     catch (Exception err) 
     { 
      System.Diagnostics.Debug.WriteLine("Exception: " + err.Message); 
     } 
     finally 
     { 
      con.CloseSqlConnection(); 
     } 
    } 

回答

5

其becasue您正在使用的DataReader,而不是使用這個數據表會爲你工作。讀者只讀和轉發這只是爲什麼只有第一dropdonw充滿數據和其他人是空的。

改變的代碼:

protected void FillTheDropDownLists() 
    { 
     SqlApplication con = new SqlApplication(); 
     try 
     { 
      SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection()); 
      con.OpenSqlConnection(); 

      SqlDataReader reader = cmd.ExecuteReader(); 
      DataTable dt = new DataTable(); 
      dt.Load(reader); 

      blackList.DataValueField = "name"; 
      blackList.DataSource = dt ; 
      blackList.DataBind(); 

      colorList1.DataValueField = "name"; 
      colorList1.DataSource = dt ; 
      colorList1.DataBind(); 

      colorList2.DataValueField = "name"; 
      colorList2.DataSource = reader; 
      colorList2.DataBind(); 

      colorList3.DataValueField = "name"; 
      colorList3.DataSource = dt ; 
      colorList3.DataBind(); 

      otherColor1.DataValueField = "name"; 
      otherColor1.DataSource = dt ; 
      otherColor1.DataBind(); 

      otherColor2.DataValueField = "name"; 
      otherColor2.DataSource = dt ; 
      otherColor2.DataBind(); 

      otherColor3.DataValueField = "name"; 
      otherColor3.DataSource = dt ; 
      otherColor3.DataBind(); 

      reader.Close(); 
     } 
     catch (Exception err) 
     { 
      System.Diagnostics.Debug.WriteLine("Exception: " + err.Message); 
     } 
     finally 
     { 
      con.CloseSqlConnection(); 
     } 
    } 
+0

非常感謝,它已經奏效,但是這次又有另外一個問題,發佈表單後,每個選定的值都是內容的第一個值。它不會選擇真正選定的值。哪裏可以解決問題? – anarhikos 2010-06-25 11:09:35

+0

檢查視圖狀態屬性,因爲我認爲它沒有持有狀態後發回 – 2010-06-25 11:16:02

+1

解決,非常感謝 – anarhikos 2010-06-25 12:15:36

1

按照documentation數據讀取器是僅正向。使用數據表或讀入列表並將您的組合連接到它。

0

輸入代碼在這裏

 for (int i = 1; i <= 260; i++) 
     { 
      ContentPlaceHolder maincontent = Page.Master.FindControl("MainContent") as ContentPlaceHolder; 
      DropDownList a = (DropDownList)maincontent.FindControl("ddl" + i); 



      a.DataSource = ds; 
      a.DataTextField = "Options"; 
      a.DataValueField = "id"; 
      a.DataBind(); 
      a.Items.Insert(0, new ListItem("Select", "0", true)); 
     } 

真正簡單的代碼。