Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: How to dynamic create "Custom Template Control"?


Message #1 by sugi@m... on Mon, 9 Dec 2002 05:49:39
After reading this book "Professional ASP.NET Server Controls - Building 
Custom Controls with C#",I have some questions.
Here are three files,as following:
file 1 :
 
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
 
namespace WebControlLibrary1.Design
{
 /// <summary>
 /// Summary description for RepeaterControl.
 /// </summary>
 [DefaultProperty("Text"), 
  ToolboxData("<{0}:RepeaterControl 
runat=server></{0}:RepeaterControl>")]
 [ParseChildren(true)]
 public class RepeaterControl : 
System.Web.UI.WebControls.WebControl, INamingContainer
 {
  private ITemplate _itemTemplate;
  private ITemplate _headerTemplate;
 
  public int RepeatCount{
   get{ return int.Parse(ViewState
["_repeatCount"].ToString()) ;}
   set{ ViewState["_repeatCount"] = 
value ;}
  }
 
  [TemplateContainer(typeof
(SimpleRepeaterItem))]
  public ITemplate ItemTemplate{
   get{ return _itemTemplate ;}
   set{ _itemTemplate = value ;}
  }
 
  [TemplateContainer(typeof
(SimpleRepeaterItem))]
  public ITemplate HeaderTemplate
  {
   get{ return _headerTemplate ;}
   set{ _headerTemplate = value ;}
  }
 
  protected override void CreateChildControls
(){
   Controls.Clear();
   if((RepeatCount>0)&&(_itemTemplate!
=null))
   {
    if(_headerTemplate!=null)
    {
     SimpleRepeaterItem 
headerContainer=new SimpleRepeaterItem();
    
 _headerTemplate.InstantiateIn(headerContainer);
    
 headerContainer.DataBind();
     Controls.Add
(headerContainer);
    }
    for(int 
i=0;i<RepeatCount;i++)
    {
     SimpleRepeaterItem 
container=new SimpleRepeaterItem(5);
    
 _itemTemplate.InstantiateIn(container);
     container.DataBind
();
     Controls.Add
(container);
    }
   }
   else
   {
    Controls.Add(new 
LiteralControl("please give the RepeatCount and 
ItemTemplate"));
   }
  }
 }
 
 public class SimpleRepeaterItem : WebControl, 
INamingContainer
 {
  private int ctlCount;
  private PRControlLibrary.UpdateData upd1;
 
  public SimpleRepeaterItem(int cc)
  {
   this.ctlCount=cc;
   this.upd1=new UpdateData();
  }
 
  public SimpleRepeaterItem()
  {
   ctlCount=0;
  }
 }
}
 
file 2 :
 
<%@ Page language="c#" Codebehind="Control1.aspx.cs" 
AutoEventWireup="false" Inherits="Test8.Control1" %>
<%@ Register TagPrefix="cc1" 
Namespace="WebControlLibrary1.Design" 
Assembly="WebControlLibrary1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 
Transitional//EN" >
<HTML>
 <HEAD>
  <title>Control1</title>
  <meta content="Microsoft Visual Studio 
7.0" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" 
name="vs_defaultClientScript">
  <meta 
content="http://schemas.microsoft.com/intellisense/ie5" 
name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Control1" method="post" 
runat="server">
   <asp:button id="Button1" style="Z-
INDEX: 101; LEFT: 356px; POSITION: absolute; TOP: 4px" 
runat="server" Text="More 5>>"></asp:button>
   <cc1:repeatercontrol 
id="RepeaterControl1" style="Z-INDEX: 102; LEFT: 20px; 
POSITION: absolute; TOP: 40px" runat="server" 
RepeatCount="5" Width="750px">
   
 <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
     <div style="BORDER-
TOP: #ff3366 thin groove; BACKGROUND-COLOR: 
#cccc66">EmpName:
     
 <asp:TextBox Runat="server" 
Width="50px"></asp:TextBox></div>
    </ItemTemplate>
   </cc1:repeatercontrol><asp:textbox 
id="TextBox1" style="Z-INDEX: 103; LEFT: 487px; POSITION: 
absolute; TOP: 4px" runat="server" 
Width="55px"></asp:textbox>
   <asp:button id="Button2" style="Z-
INDEX: 104; LEFT: 270px; POSITION: absolute; TOP: 4px" 
runat="server" Text="Check"></asp:button>
  </form>
 </body>
</HTML>
 
file 3 :
 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Common;
 
namespace Test8
{
 /// <summary>
 /// Summary description for Control1.
 /// </summary>
 public class Control1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button 
Button1;
  protected 
System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Button 
Button2;
  protected 
WebControlLibrary1.Design.RepeaterControl RepeaterControl1;
 
  private void Page_Load(object sender, 
System.EventArgs e)
  {
   // Put user code to initialize the 
page here
  }
 
  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required 
by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - 
do not modify
  /// the contents of this method with the 
code editor.
  /// </summary>
  private void InitializeComponent()
  {    
   this.Button1.Click += new 
System.EventHandler(this.Button1_Click);
   this.Button2.Click += new 
System.EventHandler(this.Button2_Click);
   this.Unload += new 
System.EventHandler(this.Page_UnLoad);
   this.Load += new 
System.EventHandler(this.Page_Load);
   this.PreRender += new 
System.EventHandler(this.Page_PreRender);
 
  }
  #endregion
 
  private void Button1_Click(object sender, 
System.EventArgs e)
  {
   RepeaterControl1.RepeatCount=10;
  }
 
  private void Page_PreRender(object sender, 
EventArgs e){
   string 
a=TextBox1.Width.Value.ToString();
  }
 
  private void Page_UnLoad(object sender, 
EventArgs e){
   string 
a=TextBox1.Width.Value.ToString();
  }
 
  private void Button2_Click(object sender, 
System.EventArgs e)
  {
   
  }
 }
}
 

I have a question is that :
First,I want to have 5 repeat ItemTemplate when I first-
in,since the property value in "RepeatCount" is 5. After 
filling the employee's name in textbox more then 5 
records, I must create anthor 5 ItemTemplate. By clicking 
the button "More 5>>",I could have 10 records in 
imagination. But ,in fact, I must click the button 
twice,it can be true. Why does it happen? In addition,I 
also want to save the viewstate and values of all textbox 
controls that had been input the employee's name,when 
clicking the button caused postback event.

  Return to Index