2013-04-23 28 views
0

我有以下的ASPX和CS如何根据ASP.NET C#中服务器端的条件克隆WebControls?

<%@ 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... 
     } 
    } 
} 

这里的要点是建立在ASPX见过多次的(列表)的ViewState [“CHECKED_CARDS_ITEMS”])表。计数。例如,如果(列表)ViewState [“CHECKED_CARDS_ITEMS”])。计数= 5,然后我想看表和它的所有内容5次(显然,每个将不同,因为服务器端I因此,使用该foreach)

如何克隆这些WebControls? 我听说我不能在这些控件上使用runat =“server”(但是我不确定在这个例子中是否可以工作),其他一些消息来源声称它应该在div里面,并克隆div。

回答

2

您可能需要一个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...); 

绑定列表列表到列表控件

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属性,让你显示你的卡水平或垂直

+0

@Victor,除了encodebiz答案,你可能会看看Repeater控件,它是列表最基本的模板控件。一个必须知道的ASP.NET开发 – jbl 2013-04-23 16:47:43