using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Initially create a master page and add it to the webform
//create a table dynamically and set the ID property of the table
Table tb1 = new Table();
tb1.ID = "table1";
//Page.Controls.Add(tb1);
//create a contentplace holder and find that control in Master page using Find Control
ContentPlaceHolder cont1 = (ContentPlaceHolder)Master.FindControl
("ContentPlaceHolder1");
//Add the table to the content place holder
cont1.Controls.Add(tb1);
//new row is created and added to the table
TableRow r1 = new TableRow();
r1.ID = "r1";
tb1.Rows.Add(r1);
//new column is created and added to the respective row
TableCell c1 = new TableCell();
r1.Cells.Add(c1);
//create the labels dynamically and add them to the respective column
Label l1 = new Label();
l1.Text = "User Name";
l1.ID = "Label1";
c1.Controls.Add(l1);
TableCell c12 = new TableCell();
c12.ID = "c12";
r1.Cells.Add(c12);
//creating the textboxes dynamically and add them to the respective column
TextBox t1 = new TextBox();
t1.ID = "textbox1";
c12.Controls.Add(t1);
TableRow r2 = new TableRow();
tb1.Rows.Add(r2);
TableCell c2 = new TableCell();
r2.Cells.Add(c2);
Label l2 = new Label();
l2.Text = "Password";
l2.ID = "label2";
c2.Controls.Add(l2);
TableCell c22 = new TableCell();
r2.Cells.Add(c22);
//for passwod textbox make the textbox mode as password
TextBox t2 = new TextBox();
t2.ID = "textbox2";
t2.TextMode = TextBoxMode.Password;
c22.Controls.Add(t2);
TableRow r3 = new TableRow();
tb1.Rows.Add(r3);
TableCell c3 = new TableCell();
c3.ColumnSpan = 2;
c3.HorizontalAlign = HorizontalAlign.Center;
r3.Cells.Add(c3);
Button b1 = new Button();
b1.Text = "Submit";
//+= shows the tab and on pressing the tab the event is fired
//b1.click is nothing but button click event
b1.Click += new EventHandler(b1_Click);
c3.Controls.Add(b1);
}
//button click event
void b1_Click(object sender, EventArgs e)
{
//in this event the text in username text displays in a label
//using recursive method
Label res = new Label();
res.ID = "result";
//FindControlRecursive finds the tetxbox control
TextBox rest = (TextBox)FindControlRecursive(this.Master, "textbox1");
res.Text = "welcome" + rest.Text;
//finds the table
Table tab1 = (Table)FindControlRecursive(this.Master, "table1");
TableRow resr = new TableRow();
tab1.Controls.Add(resr);
TableCell resc = new TableCell();
resr.Controls.Add(resc);
//adds the label control to the column and displays the message ..
resc.Controls.Add(res);
}
//this method is used where is no form tag in the aspx page as the page uses Master Page public override void VerifyRenderingInServerForm(Control control)
{
}
//this a recursive function which finds the controls recursively
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
}
Contributors
Ads
Dynamic Controls using Master Pages
under:
Dynamic Controls,
Master Pages
0 Comments Received
Post a Comment