2012-12-11 24 views
0

正如你所知道的,當你在數據庫表中有很多元素時,你也在這個表和另一個表之間存在關係(比如說多對多),當你試圖在動態數據中插入一個新項目,對於外鍵選項,您可以檢查很多複選框。用gridview替換動態數據中的複選框

舉例來說,如果你有類別和產品,你要插入一個新的類別,你將有這樣的事情:

Product1 Product2 Product3 Product4 
Product5 Product6 ... 

當你有一些產品看起來不錯,但是當你有超過50個000它不。

如何在每個產品名稱之前使用具有複選框的網格視圖(我加載產品實體)來替換它們。如果我檢查該複選框,這意味着我想將該產品添加到此類別(我在此刻創建)。

希望你明白我想說什麼。 我想製作一個自定義字段(或自定義字段的自定義頁面),但我不知道如何製作網格視圖。

謝謝

回答

0

你怎麼樣讓一個GridView:

<asp:GridView runat="server" ID="gvMyGrid"> 
     <Columns> 
      <asp:BoundField DataField="MyField" HeaderText="HeaderForMyField /> 
      ... You have a few of these 
      <asp:TemplateField HeaderText="Products"> 
        <ItemTemplate> 
         <asp:Repeater runat="server" id="rptListOfProducts" OnItemDataBound="ProductsDataBound"> 
          <ItemTemplate> 
            <asp:HiddenField id="hfProductId" runat="server" 
             Value='<%# DataBinder.Eval(Container.DataItem, "ProductId") %>'/> 
            <asp:CheckBox id="cbProduct" runat="Server" 
             Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>' 
             Checked='<%# DataBinder.Eval(Container.DataItem, "IsThisProductSelected") %>' /> //Or some kind of flag that is true or false 
          </ItemTemplate> 
         </asp:Repeater> 
        </ItemTemplate> 
     </Columns> 
    </asp:GridView> 

然後在你的C#代碼,你可以像這樣來找出用戶改變什麼。

foreach (Control c in rptMonitors.Items) 
    { 
      var productId = int.Parse(((HiddenField)c.FindControl("hfProductId")).Value); 
      //And get the check box too, generate a list of ProductIds, and if they're checked or not 
    } 

不知道這是否就是你要找的東西,這說明有點含糊

編輯:哦,如果你希望條目的負載中Repeater,你可以將它包裝在一個AjaxControlToolkit可摺疊面板,所以它不會將您的屏幕垃圾郵件。

+0

其實,回頭看看我寫的東西,CheckBoxList控件會比Repeater更簡單 –

+0

是的。類似的東西,但我如何加載產品到該GridView? – Kosmog

+0

@ user1607079只需設置你的gvMyGrid.DataSource = myObj;然後gvMyGrid.DataBind(); 您綁定的對象只需具有與您要拔出的字段相匹配的屬性,對於Repeater –