2014-05-23 16 views
0

我想要做的是綁定和下拉列表裏面的中繼器,但我沒有做到。我在標記的行出現以下錯誤:System.NullReferenceException。任何幫助將被appriciated。ASP:NET綁定下拉列表裏面的中繼器

後端:

protected void shoeRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     FixbayDBDataContext db = new FixbayDBDataContext(); 
     var colors = from c in db.ColorTbls select new { ColorID = c.ColorID, ColorName = c.ColorName, }; 

     DropDownList ddl = (DropDownList)e.Item.FindControl("colorList1"); 
     ddl.DataSource = colors; //ERROR LINE 
     ddl.DataTextField = "ColorName"; 
     ddl.DataValueField = "ColorName"; 
    } 

前端:

<ajaxToolkit:TabPanel ID="TabPanel5" runat="server"> 
       <HeaderTemplate>Show Shoes</HeaderTemplate> 
       <ContentTemplate runat="server">                
        <asp:Repeater ID="shoeRepeater" OnItemCreated="shoeRepeater_ItemDataBound" runat="server"> 
         <HeaderTemplate></HeaderTemplate> 
         <ItemTemplate> 
          <table border="1" style="border-color:#ff9900; width:400px; font-weight:bold; font-family:'Oswald', Arial, sans-serif;"> 
           <tr> 
            <td rowspan="6" style="width:150px; height:150px;"> 
             <image src="shoeImages/<%#DataBinder.Eval(Container.DataItem,"ImagePath") %>"></image> 
            </td> 
           </tr> 
           <tr> 
            <td> 
             <%#DataBinder.Eval(Container.DataItem,"BrandName") %> <%#DataBinder.Eval(Container.DataItem,"ModelName") %> 
            </td> 

           </tr> 
           <tr> 
            <td> 
             Price: $<%#DataBinder.Eval(Container.DataItem,"Price") %> 
            </td> 

           </tr> 
           <tr> 
            <td> 
             Size: <%#DataBinder.Eval(Container.DataItem,"Size") %> 
            </td> 
           </tr> 
           <tr> 
            <td> 
             <asp:DropDownList ID="colorList1" onchange="get(this)" runat="server"> 
             </asp:DropDownList> 
             Color: <%#DataBinder.Eval(Container.DataItem,"PrimaryColor") %> - <%#DataBinder.Eval(Container.DataItem,"SecondaryColor") %> 
            </td> 
           </tr> 
           <tr> 
            <td> 
             Quantity: <%#DataBinder.Eval(Container.DataItem,"Quantity") %> 
            </td> 

           </tr> 
          </table> 
         </ItemTemplate> 
         <FooterTemplate></FooterTemplate> 
        </asp:Repeater> 
       </ContentTemplate> 
      </ajaxToolkit:TabPanel> 
+0

檢查你的顏色是否給出了空例外。 – tarzanbappa

+0

@tarzanbappa我使用相同的linq sql查詢中繼器外的另一個ddl。它工作正常。 – Tartar

+0

然後調試並檢查您獲得的** ddl **的值。是否爲null? – tarzanbappa

回答

1

你已經訂閱了錯誤的事件:

OnItemCreated="shoeRepeater_ItemDataBound" 

這應該是

OnItemDataBound="shoeRepeater_ItemDataBound" 
+0

我試過這個,但我得到了同樣的錯誤。 – Tartar

1

下面是解

protected void shoeRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     FixbayDBDataContext db = new FixbayDBDataContext(); 
     var colors = from c in db.ColorTbls select new { ColorID = c.ColorID, ColorName = c.ColorName, }; 

     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      DropDownList ddl = (DropDownList)e.Item.FindControl("colorList1"); 
      ddl.DataSource = colors;//Or any other datasource. 
      ddl.DataTextField = "ColorName"; 
      ddl.DataValueField = "ColorName"; 
      ddl.DataBind(); 
     } 
    } 
+0

很高興看到你找到答案:) – tarzanbappa