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"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
 <title>Untitled Page</title>  
</head>  
<body>  
 <form id="form1" runat="server">  
     <div>  
         <asp:TextBox ID="txtNumber" runat="server" BackColor="#FF8000"  
ForeColor="#FF8000"></asp:TextBox>  
         <asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" />  
         <asp:Repeater ID="rpt" runat="server">  
             <HeaderTemplate>  
                 <table>  
             </HeaderTemplate>  
             <ItemTemplate>  
                 <tr>  
                     <td>  
                         <asp:TextBox ID="txt" runat="server"></asp:TextBox>  
                     </td>  
                     <td>  
                         <asp:TextBox ID="txt1" runat="server"></asp:TextBox>  
                     </td>  
                 </tr>  
             </ItemTemplate>  
             <FooterTemplate>  
                 </table></FooterTemplate>  
         </asp:Repeater>  
     </div>  
 </form>  
</body>  
</html>  

  
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++)  
       {  
           dataSource.Add(i);  
       }  
  
       this.rpt.DataSource = dataSource;  
       this.rpt.DataBind();  
  
  
   }  
}

إرسال تعليق

Please do not post any spam link in the comment box😊

أحدث أقدم

Blog ads

CodeGuru