Tuesday, November 22, 2011

URL mapping in ASP.NET

Wednesday, November 16, 2011

Create a table at runtime


//CREATING A TABLE
public void createtabel()
{
//create table body i.e, <table>
Table table = new Table();
//Add table properties
table.BackColor = System.Drawing.Color.Gray;
table.Width = Unit.Percentage(100);
table.Height = Unit.Percentage(100);


//Create table row i.e,<tr>
TableRow tablerow = new TableRow();
tablerow.VerticalAlign = VerticalAlign.Top;


//create table cell i.e,<td>1
TableCell tablecell1 = new TableCell();
//tablecell1.Controls.Add();
//add <td> to <tr>
tablerow.Cells.Add(tablecell1);
//create table cell i.e,<td>2
TableCell tablecell2 = new TableCell();
//add <td> to <tr>
tablerow.Cells.Add(tablecell2);
//create table cell i.e,<td>3
TableCell tablecell3 = new TableCell();

//add <td> to <tr>
tablerow.Cells.Add(tablecell3);


//**Add the Table <tr> to Tabale <table>
table.Rows.Add(tablerow);
//*****Add the table control to the page
Page.Controls.Add(table);

}

==================================
Based on gien value of ROWS and COLUMNS




private void CreateDynamicTable()
{

PlaceHolder1.Controls.Clear();
// Fetch the number of Rows and Columns for the table

// using the properties

int tblRows = 3;

int tblCols = 5;

// Create a Table and set its properties

Table tbl = new Table();
tbl.Attributes.Add("style", "Height:100px; width:100px; border:2px solid red; background:gray;");

// Add the table to the placeholder control

PlaceHolder1.Controls.Add(tbl);
//Page.Controls.Add(tbl);

// Now iterate through the table and add your controls

for (int i = 0; i < tblRows; i++)
{

TableRow tr = new TableRow();

for (int j = 0; j < tblCols; j++)
{

TableCell tc = new TableCell();

TextBox txtBox = new TextBox();

txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;

// Add the control to the TableCell

tc.Controls.Add(txtBox);

// Add the TableCell to the TableRow

tr.Cells.Add(tc);

}

// Add the TableRow to the Table

tbl.Rows.Add(tr);

}
PlaceHolder1.Controls.Add(tbl);


// This parameter helps determine in the LoadViewState event,

// whether to recreate the dynamic controls or not



ViewState["dynamictable"] = true;

}





===========================================
Add CSS Propeties Dynamically

-----------

WebPartZone webPartZone = new WebPartZone();
webPartZone.ID = aZoneTitle.Replace(" ", "");
webPartZone.HeaderText = "";// aZoneTitle;
webPartZone.Attributes.Add("style", "height:1000px; width:30%; border:0px solid red; float:left;");
webPartZone.EmptyZoneText = "";



=====================
Note:
Posiible Error: 
Control 'TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server.


Solution:
To Add any Server controls like TextBox or Button in table cell we have use one PlaceHolder control from toolbox and in code behind  add the table control to placeholder as shown below:

PlaceHolder1.Controls.Add(tbl);