Create Dynamic Control using Repeater and List-ASP.NET

Creating dynamic control in ASP.NET WebForm and reading their values is a little bit complex, but In this article, I will show you an easy step to create dynamic control in ASP.NET with the help of ASP.NET Repeater Control. Repeater Control In ASP.NET provides flexibility to control the layout. Check out the following code snippet its very easy to understand

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTextBox.aspx.cs"  
Inherits="DynamicTextBox" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns="">  
<head runat="server">  
 <title>Untitled Page</title>  
 <form id="form1" runat="server">  
         <asp:TextBox ID="txtNumber" runat="server" BackColor="#FF8000"  
         <asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" />  
         <asp:Repeater ID="rpt" runat="server">  
                         <asp:TextBox ID="txt" runat="server"></asp:TextBox>  
                         <asp:TextBox ID="txt1" runat="server"></asp:TextBox>  

using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
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 System.Collections.Generic;  
public partial class DynamicTextBox : System.Web.UI.Page  
   protected void Page_Load(object sender, EventArgs e)  
   protected void btnCreate_Click(object sender, EventArgs e)  
       int numberOfTextboxes = int.Parse(txtNumber.Text);  
       List<int> dataSource = new List<int>();  
       for (int i = 0; i < numberOfTextboxes; i++)  
       this.rpt.DataSource = dataSource;  
Next Post Previous Post
No Comment
Add Comment
comment url