SQL数据绑定到中继器C#中的下拉列表

时间:2010-12-14 20:32:41

标签: c# asp.net sql data-binding

好的,所以我试图将数据绑定到c#的下拉列表中。尝试将数据输入DDL时,我收到Null错误。我正在使用此代码作为前端。

 <asp:Repeater ID="RepeaterHardDrives" runat="server">
                    <HeaderTemplate>
                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:HiddenField ID="hidHardDrivesPackageDefaultID" runat="server" />
                                <asp:HiddenField ID="hidHardDrivesPackageDefaultPrice" runat="server" />
                                <span>
                                    <asp:Label runat="server" ID="lbHardDiskPrice" Text="$00.00/mo"></asp:Label></span><label>Hard
                                        Drive:</label><asp:DropDownList ID="ddHardDrive" DataTextField="ItemName" DataValueField="ProductItemID" runat="server" CssClass="lidropdown">
                                        </asp:DropDownList>
                                <asp:ImageButton runat="server" ID="ShowHarddriveInfo" ImageUrl="/_Images/server_configurator_helpbutton.png"
                                    OnClick="lnkShowHarddriveInfo_OnClick" /></div>
                            </td>
                            <td align="right">
                                <asp:Label runat="server" ID="lbHardDrivesPrice" />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                        <br />
                    </FooterTemplate>
                </asp:Repeater>

对于后端我试图将动态数量的Dropdown列表加载到转发器中,然后用相同的数据对它们进行数据绑定。

  public void PopulateHardDrives(int intSupportedDrives)
  {
    PreloadHardDriveRepeater(intSupportedDrives);

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["connstrname"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Prod_SelectIDNamePriceByCategory", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CategoryCode", "Hard Drive");
    DataTable dtHardDrives = new DataTable();
    using (conn)
    {
      conn.Open();
      SqlDataReader dr = cmd.ExecuteReader();
      dtHardDrives.Load(dr);
      ViewState.Add("dtHardDrives", dtHardDrives); 
    }

    foreach (RepeaterItem riHardDrive in RepeaterHardDrives.Items)
    {
      DropDownList ddHardDrives = (DropDownList)riHardDrive.FindControl("ddHardDrives");
      ddHardDrives.DataSource = dtHardDrives;//program gives NULL exception error here(object not set to instance of object however it know the count of the rows it is supposed to be pulling)
      ddHardDrives.DataValueField = "ProductItemID";
      ddHardDrives.DataTextField = "ItemName";
      ddHardDrives.DataBind();
      Label lbHardDrive = (Label)riHardDrive.FindControl("lbHardDrivesPrice");
      lbHardDrive.Text = String.Format("{0:c}", Convert.ToDecimal("0.00"));
      if (riHardDrive.ItemIndex != 0) //We do not want to allow None to be selected on the main drive
      {
        ddHardDrives.Items.Insert(0, "None");
      }
    }
  }

and last but not least the function to setup the dynamic amount of DDL's looks like this 

     private void PreloadHardDriveRepeater(int intSupportedDrives)
    {
        int[] intArrDisks = new int[intSupportedDrives];
        for (int intDiskCount = 0; intDiskCount < intArrDisks.Length; intDiskCount++)
        {
            intArrDisks[intDiskCount] = intDiskCount;
        }
        RepeaterHardDrives.DataSource = intArrDisks;
        RepeaterHardDrives.DataBind();
    }

我在!page.isPostBack if语句中调用了一个填充函数列表,唯一一个没有获取数据的函数是带有“淹没列表”的函数。它从数据库中获取行数(18),但是它抛出了一个Null错误(对象引用没有设置为对象的实例。)我看到很多人在谷歌搜索问题时遇到了这个错误但是我找不到适合我的解决方案。 PreloadHardDriveRepeater函数在单独运行时似乎工作正常,它会将正确数量的DDL加载到页面上。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您的控件是“ddHardDrive”:

<asp:DropDownList ID="ddHardDrive" DataTextField="ItemName" DataValueField="ProductItemID" runat="server" CssClass="lidropdown">

您的代码正在寻找“ddHardDrives”

riHardDrive.FindControl("ddHardDrives");

如果您调试函数并在抛出异常之前查看了变量值,这将很容易注意到。