如何克隆WebControls取决于条件在服务器端在ASP.净c#
本文关键字:服务器端 ASP 条件 何克隆 WebControls 取决于 | 更新日期: 2023-09-27 18:10:26
我有以下的aspx和cs
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cardPrintingBarcode.aspx.cs" Inherits="Reports_cardPrintingBarcode" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>
Impresión de Tarjeta
</title>
<style type="text/css">
.style2
{
font-size: xx-small;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="Card">
<table>
<tr>
<td width="85px" align="center" valign="bottom">
<image ID="imgBarcode" Width="85px" Height="20px" />
<br />
<label ID="lblCardcode" Font-Bold="True" Font-Names="Arial" Font-Size="5pt" >
</label>
</td>
<td width="30px" />
<td width="40px" align="center" valign="bottom">
<label ID="lblCN" Font-Bold="True" Font-Names="Arial" Font-Size="7pt">
</label>
<br />
<image ID="imgQR" Width="37px" Height="37px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClubCard.Core;
public partial class Reports_cardPrintingBarcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["CHECKED_CARDS_ITEMS"] != null)
{
if (((List<int>)ViewState["CHECKED_CARDS_ITEMS"]).Count > 0)
{
LoadCards();
}
else
{
this.Response.Redirect("~/default.aspx");
//GET ME OUT OF HERE
}
}
}
private void LoadCards()
{
List<int> lstIdCards = (List<int>)ViewState["CHECKED_CARDS_ITEMS"];
foreach (int intIdCard in lstIdCards)
{
//Process Where I Want To Assign Data To 2 Labels and 2 images
ClubCard.Core.Card card = new ClubCard.Core.Card(intIdCard);
ClubCard.Core.Client client = new ClubCard.Core.Client(card.idClient);
Byte[] bcQR = QR.GetQRByteArray(card.cardCode);
string strBase64 = Convert.ToBase64String(bcQR, 0, bcQR.Length);
//lblCN.Text = client.number;
//lblCardCode.Text = card.number;
//imgBarcode.ImageUrl = "SOME URL";
//imgQR.ImageUrl = "SOME URL";
//PROBABLY HERE'S WHERE I HAVE TO CREATE ALL WEBCONTROLS...
//THEN WHEN THE FOREACH STARTS AGAIN, CREATE ANOTHER ONE...
}
}
}
这里的要点是创建与(List)ViewState["CHECKED_CARDS_ITEMS"]). count一样多的在aspx中看到的表。
例如…如果列表视图状态("CHECKED_CARDS_ITEMS"))。Count = 5,然后我想看到表和它的所有内容5次(显然,每次都会不同,因为服务器端我给每个分配不同的数据,因此,使用foreach)
如何克隆这些WebControls?我听说我不能在这些控件上使用runat="server"(但是我不确定在这个例子中是否有效),其他一些来源声称它应该在div中,并克隆div。
您可能需要一个ListControl,例如<asp:DataList />
, <asp:GridView
等
创建一个卡片列表
List<ClubCard.Core.Card> cards = new List<ClubCard.Core.Card>();
//cards.Add(...some cards...);
//cards.Add(...some cards...);
//cards.Add(...some cards...);
将列表绑定到ListControl
cardListControl.DataSource = cards;
cardListControl.DataBind();
用DataList实现这个:
<asp:DataList runat="server" ID="cardListControl">
<ItemTemplate>
<!-- Layout to display card here -->
<img src='<%# Eval("CardImageUrl")>' />
<span><%# Eval("CardName") %></span>
<!-- other card information -->
</ItemTemplate>
</asp:DataList>
DataList有一个RepeatDirection
属性,可以让你显示你的卡片水平或垂直