c# - 将相同的数据源分配给多个 DropDownLists?

标签 c# asp.net

我想将相同的数据源分配给表单中的多个下拉列表,当页面加载时,只有第一个下拉列表填充了数据源的内容,其他都是空的,缺少什么?感谢您的回答。这是代码;

<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();
        }
    }

最佳答案

因为您使用的是 dataReader 而不是这个 use datatable 才适合您。 Reader 是只读的,只能转发,这就是为什么只有第一个 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();
        }
    }

关于c# - 将相同的数据源分配给多个 DropDownLists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3117314/

相关文章:

c# - 根据创建时间/反转Excel工作表以相反的顺序对Excel工作表进行排序

asp.net - 一个页面可以继承多个母版页吗(ASP.NET)

asp.net - 将 Azure 管道与 Gitlab 集成(持续集成)

asp.net - Dropzone 未绑定(bind)到模型

c# - 检查以逗号分隔的小数位字符串 - C#

c# - 当数据对于 web.config 来说太大时该怎么办

c# - Windows 窗体应用程序中的多线程调用?

c# - Visual Studio 可以等到我下线后才显示问题吗?

javascript - 无法处理 jqGrid 中的复选框单击事件

c# - 将页码添加到 pdf 文档 (itextsharp)