2010-12-14 81 views
0

好吧,所以我試圖將數據綁定到c#的下拉列表中。嘗試將數據輸入到DDL時出現空錯誤。我正在使用此代碼作爲前端。中繼器中的數據綁定到下拉列表C#

<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> 

爲後端我試圖加載到轉發器的動態數量的下拉列表,然後用相同的數據綁定它們。

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),但它會拋出一個空錯誤(對象引用未設置爲對象實例)。我看到不少人在搜索問題時遇到了這個錯誤,但是我找不到適合我的解決方案。 PreloadHardDriveRepeater函數在單獨運行時似乎工作正常,它會將正確數量的DDL加載到頁面上。

提前致謝。

回答

1

你的控制是「ddHardDrive」:

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

和你的代碼是尋找「ddHardDrives」

riHardDrive.FindControl("ddHardDrives"); 

這將是很容易發現,如果你調試到功能,看着你在拋出異常之前的變量值。

+0

大聲笑很好,工作很好(難怪沒有我發現的例子工作)。謝謝你借給我你的眼睛 – h34dhun73r 2010-12-14 20:47:57