C# 在中继器网格视图中标记复选框
所以基本上我有两个表,一个是productVariants,一个是DistributionStandardPackingUnitItems。在我的DistributionStandardPackingUnitItems中,它存储要打包的productVariant id和数量。我想做的是,如果产品变量id作为一个项目存在于distributionStandardPackingUnitItems中,那么该行的复选框将被标记。以下是我如何在中继器内设置网格视图:C# 在中继器网格视图中标记复选框,c#,asp.net,repeater,C#,Asp.net,Repeater,所以基本上我有两个表,一个是productVariants,一个是DistributionStandardPackingUnitItems。在我的DistributionStandardPackingUnitItems中,它存储要打包的productVariant id和数量。我想做的是,如果产品变量id作为一个项目存在于distributionStandardPackingUnitItems中,那么该行的复选框将被标记。以下是我如何在中继器内设置网格视图: <asp:Repeater I
<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();
}
}
}
}
}
}
protectedvoid Repeater1\u ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
如果(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.AlternatingItem)
{
Label lblProduct=(Label)e.Item.FindControl(“lblProduct”);
product=lblProduct.Text;
//对项目和交替项目执行以下逻辑
如果(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.AlternatingItem)
{
GridView gv=(GridView)e.Item.FindControl(“gvProduct”)作为GridView;
如果(gv!=null)
{
//根据类别获取产品列表,并将数据绑定到网格视图
prodVariantList=prodPackBLL.getAllProductVariantByProduct(产品);
gv.DataSource=prodVariantList;
gv.DataBind();
//对于包含在标准装箱单中的产品,选中复选框
List distSPUItemList=新列表();
distSPUItemList=packBLL.getAllDistSpuitemByDistributionDNSpuName(distributionID,SPUname);
//LINQ查询,用于比较查找ProductPacking对象的对象列表,这些对象的名称属性等于项目列表中的项目
var available=来自prodVariantList中的
//在prodList和SPUItemList之间执行内部联接,仅返回名称匹配的对象
//(x=>x.name==a.name)将b.name与a.name进行比较,这是强制实现名称匹配的具体方法
来自distSPUItemList中的b,其中(x=>x.id==a.id)
//完成内部连接后,仅从prodList中选择对象
选择一个;
//迭代gridview行
GridView gvForCheckBox=(GridView)e.Item.FindControl(“gvProduct”)作为GridView;
foreach(gvForCheckBox.Rows中的GridViewRow gr)
{
//如果prodList和SPUItemList中项目的产品名称匹配
if(可用。其中(x=>x.id==gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString()).Any())
{
//选中复选框
复选框cb=(复选框)gr.Cells[0]。FindControl(“cbCheckRow”);
cb.Checked=真;
//按产品名称获取产品包装数量
字符串名称=gr.Cells[1]。文本;
int productQuantity=packBLL.getProductQuantityByName(名称);
TextBox tb=(TextBox)gr.Cells[3]。FindControl(“tbQuantity”);
tb.Text=productQuantity.ToString();
}
}
}
}
}
}
如何获取项目列表中的项目:
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;
}
public List getAllDistributsPuItemByDistributionIdnsPuName(string distributionID,string SPUname)
{
List itemList=新列表();
使用(var connection=newsqlconnection(FoodBankDB.connectionString))
{
SqlCommand command=new SqlCommand(“从dbo.DistributionStandardPackingUnitItems dspui中选择pv.id、p.name、pc.categoryName、dspui.productQuantity”+
“dspui上的内部联接dbo.DistributionStandardPackingUnits dspu.distributionStandardPackingUnit=dspu.id”+
“dspui.productVariant=pv.id上的内部联接dbo.ProductVariants pv”+
“内部联接dbo.Products p ON pv.product=p.id”+
“p.productCategory=pc.id上的内部联接dbo.ProductCategories pc”+
“其中dspu.distribution='”
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;
}