在转发器网格视图中标记复选框

时间:2014-01-22 09:07:26

标签: c# asp.net repeater

所以基本上我有两个表,一个是productVariants,一个是DistributionStandardPackingUnitItems。在我的DistributionStandardPackingUnitItems中,它存储productVariant id和要打包的数量。我想要做的是,如果产品变量id作为distributeStandardPackingUnitItems中的项存在,则将标记该行的复选框。以下是我在转发器中设置网格视图的方法:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
                    <ItemTemplate>
                        <!-- COLLAPSIBLE PANEL EXTENDER -->
                        <asp:Panel ID="pHeader1" runat="server" CssClass="cpHeader">
                            <!-- Collapsible panel extender header -->
                            <div class="form-group" style="background-color: #ffb848; height: 30px; vertical-align: middle">
                                <div class="col-md-6">
                                    <div style="float: left; color: White; padding: 5px 5px 0 0">
                                        <asp:Label ID="lblProduct" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' runat="server" />
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div style="float: right; color: White; padding: 5px 5px 0 0">
                                        <asp:Label ID="lblHeaderText1" runat="server" />
                                    </div>
                                </div>
                                <div style="clear: both"></div>
                            </div>
                        </asp:Panel>
                        <!-- Collapsible panel extender body -->
                        <asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
                            <asp:Label ID="lblBodyText1" runat="server" />
                            <!-- Grid view to show products based on each category -->
                            <asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="725px" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="cbCheckRow" runat="server" AutoPostBack="true" ItemStyle-Width="50px"/>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="id" HeaderText="Name" ItemStyle-Width="50px" />
                                    <asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="450px" />
                                    <asp:BoundField DataField="inventoryQuantity" HeaderText="Total Unit" />
                                    <asp:BoundField DataField="unitQuantity" HeaderText="Unit Quantity" />
                                    <asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200px">
                                        <ItemTemplate>
                                            <asp:TextBox ID="tbQuantity" runat="server" Width="40" Text="0" OnTextChanged="tbQuantity_TextChanged" AutoPostBack="true"/>
                                            <asp:Label ID="lblCheckAmount" runat="server" ForeColor="#a94442"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                </Columns>
                            </asp:GridView>
                        </asp:Panel>
                        <asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
                            ExpandControlID="pHeader1" Collapsed="true" TextLabelID="lblHeaderText1" CollapsedText="Show"
                            ExpandedText="Hide" CollapsedSize="0"
                            ScrollContents="false">
                        </asp:CollapsiblePanelExtender>
                    </ItemTemplate>
                </asp:Repeater>

我如何调用绑定的行项并选中复选框:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lblProduct = (Label)e.Item.FindControl("lblProduct");
            product = lblProduct.Text;

            //Execute the following logic for Items and Alternating Items
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                GridView gv = (GridView)e.Item.FindControl("gvProduct") as GridView;
                if (gv != null)
                {
                    //Get list of products based on category and bind data to grid view
                    prodVariantList = prodPackBLL.getAllProductVariantByProduct(product);
                    gv.DataSource = prodVariantList;
                    gv.DataBind();

                    //Mark checkbox checked for products included in standard packing list
                    List<DistributionStandardPackingUnitItems> distSPUItemList = new List<DistributionStandardPackingUnitItems>();
                    distSPUItemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);

                    //LINQ query to compare lists of objects finding ProductPacking objects whose name property are equal to the items in SPUItemList
                    var available = from a in prodVariantList
                                    // perform an inner-join between prodList and SPUItemList only returning objects whose names match
                                    // (x => x.name == a.name) compares b.name to a.name, this is specifically what enforces that names match
                                    from b in distSPUItemList.Where(x => x.id == a.id)
                                    // after inner joining is done, only select objects from prodList
                                    select a;

                    //Iterate the gridview rows
                    GridView gvForCheckBox = (GridView)e.Item.FindControl("gvProduct") as GridView;
                    foreach (GridViewRow gr in gvForCheckBox.Rows)
                    {
                        //If product name of items in prodList and SPUItemList matches
                        if (available.Where(x => x.id == gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString()).Any())
                        {
                            //Mark the checkBox checked
                            CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                            cb.Checked = true;

                            //Get the product packaging quantity by productName
                            string name = gr.Cells[1].Text;
                            int productQuantity = packBLL.getProductQuantityByName(name);
                            TextBox tb = (TextBox)gr.Cells[3].FindControl("tbQuantity");
                            tb.Text = productQuantity.ToString();
                        }
                    }
                }
            }
        }
    }

我如何获取distSPUItemList中的项目:

public List<DistributionStandardPackingUnitItems> getAllDistSPUItemByDistributionIDnSPUname(string distributionID, string SPUname)
    {
        List<DistributionStandardPackingUnitItems> itemList = new List<DistributionStandardPackingUnitItems>();

        using (var connection = new SqlConnection(FoodBankDB.connectionString))
        {
            SqlCommand command = new SqlCommand("SELECT pv.id, p.name, pc.categoryName, dspui.productQuantity FROM dbo.DistributionStandardPackingUnitItems dspui " +
                " INNER JOIN dbo.DistributionStandardPackingUnits dspu ON dspui.distributionStandardPackingUnit = dspu.id " +
                " INNER JOIN dbo.ProductVariants pv ON dspui.productVariant = pv.id " +
                " INNER JOIN dbo.Products p ON pv.product = p.id " +
                " INNER JOIN dbo.ProductCategories pc ON p.productCategory = pc.id " +
                " WHERE dspu.distribution = '" + distributionID + "' AND dspu.name = '" + SPUname + "'" +
                " ORDER BY pc.categoryName", connection);
            connection.Open();
            using (var dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    string id = dr["id"].ToString();
                    string name = dr["name"].ToString();
                    string categoryName = dr["categoryName"].ToString();
                    string productQuantity = dr["productQuantity"].ToString();

                    itemList.Add(new DistributionStandardPackingUnitItems(id, name, "", categoryName, productQuantity));
                }
            }
        }

        return itemList;
    }

以及如何获取prodVariantList中的项目:

 public List<ProductPacking> getAllProductVariantByProduct(string product)
    {
        List<ProductPacking> prodAll = new List<ProductPacking>();

        using (var connection = new SqlConnection(FoodBankDB.connectionString))
        {
            SqlCommand command = new SqlCommand("SELECT pv.id, pv.description, pv.unitQuantity, p.inventoryQuantity FROM dbo.ProductVariants pv " +
                " INNER JOIN dbo.Products p ON pv.product = p.id " +
                " WHERE p.name = '" + product + "'", connection);
            connection.Open();
            using (var dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    string id = dr["id"].ToString();
                    string name = dr["description"].ToString();
                    int unitQuantity = Convert.ToInt32(dr["unitQuantity"].ToString());
                    string inventoryQuantity = (dr["inventoryQuantity"].ToString());

                    prodAll.Add(new ProductPacking(id, name, unitQuantity, inventoryQuantity));
                }
            }
        }

        return prodAll;
    }

但是,对于distributedStandardPackingUnitItem中存在的产品变体,复选框不会被标记为已选中。我想知道为什么会这样。

提前致谢。

0 个答案:

没有答案