2012-05-22 35 views
0
private void BindGridToppings() 
    { 

    } 

這裏是從ASP頁面小型Web應用程序,添加轉發器網格項目動態

<asp:GridView ID="gridv" runat="server" AutoGenerateColumns="false" EnableModelValidation="true" OnRowDataBound="Pizzas_RowBound"> 
    <Columns> 
     <asp:TemplateField HeaderText="Edit"> 
      <ItemTemplate> 
       <asp:ImageButton ImageUrl="~/images/edit.png" ID="btnEditPizza" runat="server" RowIndex='<%# Container.DisplayIndex %>' onClick="Pizzas_RowEditing" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Delete"> 
      <ItemTemplate> 
       <asp:ImageButton ImageUrl="~/images/del.png" ID="btnDeletePizza" runat="server" RowIndex='<%# Container.DisplayIndex %>' OnClick="Pizzas_RowDeleting" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
     <asp:TemplateField HeaderText="Pizza ID" > 
      <ItemTemplate> 
       <asp:Label ID="lblID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ID").ToString() %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Crust"> 
      <ItemTemplate> 
       <asp:Label ID="lblCrust" runat="server" Text='<%#GetNameByLookUpID(DataBinder.Eval(Container.DataItem, "Crust").ToString()) %>'>   </asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Cheese"> 
      <ItemTemplate> 
       <asp:Label ID="lblCheese" runat="server" Text='<%#GetNameByLookUpID(DataBinder.Eval(Container.DataItem ,"Cheese").ToString()) %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> 
     <asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" DataFormatString="{0:C}" /> 
     <asp:TemplateField HeaderText="Toppings"> 
      <ItemTemplate> 
       <asp:Repeater ID="rptList" Runat="server">        
       </asp:Repeater> 
      </ItemTemplate>     
     </asp:TemplateField> 
    </Columns>     
</asp:GridView> 

SQL和亞音速作爲後端GridView控件,我纔開始兩個星期前與數據庫編程。所以訪問它對我來說仍然是新的。

我的DB.Where ...... call在BindGridToppings函數中被錯誤地格式化爲complimer告訴我的。或亞音速查詢不是一個可以被整合的類,因爲我的編譯器告訴我,當我嘗試一個foreach循環和.items.add

任何關於如何綁定這個中繼器的建議是可愛的。

感謝,MACAIRE貝爾

回答

0

找到了解決方案。

protected void Pizzas_RowBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      int pizzaID = Convert.ToInt32(((Label)e.Row.FindControl("lblID")).Text); 
      BulletedList bltTopping = ((BulletedList)e.Row.FindControl("bltTopping")); 
      ToppingsCollection toppings = ToppingsMembers.RetriveByPizzaID(pizzaID); 
      bltTopping.Items.Clear(); 
      foreach (Toppings topping in toppings) 
      { 
      bltTopping.Items.Add(new ListItem(GetNameByLookUpID(topping.ToppingID.ToString()))); 
     } 
    } 
} 

  <asp:TemplateField HeaderText="Toppings"> 
       <ItemTemplate>     
        <asp:BulletedList ID="bltTopping" Runat="server">        
        </asp:BulletedList> 
       </ItemTemplate>     
      </asp:TemplateField> 

在前面

謝謝你你的幫助,雖然它導致我我的答案!

0

我不太明白你在說什麼在這裏...

,要麼是我DB.Where ......呼叫格式不正確的BindGridToppings的功能與complimer告訴我的一樣。或亞音速查詢是不是可以interated一類,因爲我的編譯器告訴我,當我嘗試了foreach循環和.items.add

我看到BindGridToppings()沒有呼叫?您是否試圖解決這些編譯器錯誤(如果需要,我們需要更多的信息),或者在GridView中爲Repeater獲取正確的綁定技術?

如果你的問題是如何解決在GridView中綁定一個Repeater,試試這個。

ASPX這樣的:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" onrowdatabound="GridView1_RowDataBound"> 
     <Columns> 
      <asp:BoundField HeaderText="Foo1" DataField="Foo1" /> 
      <asp:TemplateField HeaderText="FooFive"> 
       <ItemTemplate> 
        <asp:Repeater ID="Repeater1" runat="server"> 
         <ItemTemplate> 
          <asp:Label ID="Label1" runat="server" Text='<%# Eval("FooFive") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:Repeater> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

代碼背後是這樣的:

protected void Page_Load(object sender, EventArgs e) 
    { 
     List<object> foo1Objects = new List<object>(); 
     foo1Objects.Add(new { Foo1 = "Hello" }); 
     foo1Objects.Add(new { Foo1 = "World" }); 

     GridView1.DataSource = foo1Objects; 
     GridView1.DataBind(); 
    } 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     Repeater rep1 = e.Row.FindControl("Repeater1") as Repeater; 
     if (rep1 != null) 
     { 
      List<object> fooFiveObjects = new List<object>(); 
      fooFiveObjects.Add(new { FooFive = "Apple" }); 
      fooFiveObjects.Add(new { FooFive = "Orange" }); 
      fooFiveObjects.Add(new { FooFive = "Banana" }); 

      rep1.DataSource = fooFiveObjects; 
      rep1.DataBind(); 
     } 
    } 

我希望您的數據檢索和GridView1數據綁定工作方式略有不同,但關鍵是處理GridView1_RowDataBound事件綁定中繼器。

相關問題