For Asp.net Training
Click Here
Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com
Create Controls at
Runtime in Asp.net
In our working sometimes we need to create control at run
time instead of compile time. In this article I am discussing how to create
control at runtime.
Remember your first program in asp.net where you create sum
of two numbers. At that time you create control at the compile time and show
output on button click.
Same example I am taking here but I am creating control at
runtime.
For Example:-
Take one panel at your design page so that you can add
controls at runtime. Now check the following code:-
Default.aspx
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
//declare the object of table, Table row and Table Cell.
Table TechAltum_Table = new
Table();
TableRow TechAltum_Row;
TableCell TechAltum_Cell;
//now create your first row
TechAltum_Row = new TableRow();
//now create
your first cell
TechAltum_Cell = new TableCell();
//Now add message in the cell
TechAltum_Cell.Text = "Enter First
Number";
//now add this cell in the row
TechAltum_Row.Cells.Add(TechAltum_Cell);
//now create second cell where we add one textbox
TechAltum_Cell = new TableCell();