ASP.NET Dynamically creating HTMLTableColumns (headers) - asp.net

is it possible to dynamically create html table columns (< th>< /th>) using namespae System.Web.UI.HtmlControls ?

try this:
HtmlTable table1 = new HtmlTable();
// Set the table's formatting-related properties.
table1.Border = 1;
table1.CellPadding = 3;
table1.CellSpacing = 3;
table1.BorderColor = "red";
// Start adding content to the table.
HtmlTableRow row;
HtmlTableCell cell;
for (int i=1; i<=5; i++)
{
// Create a new row and set its background color.
row = new HtmlTableRow();
row.BgColor = (i%2==0 ? "lightyellow" : "lightcyan");
for (int j=1; j<=4; j++)
{
// Create a cell and set its text.
cell = new HtmlTableCell();
cell.InnerHtml = "Row: " + i.ToString()+ "<br />Cell: " + j.ToString();
// Add the cell to the current row.
row.Cells.Add(cell);
}
// Add the row to the table.
table1.Rows.Add(row);
}
// Add the table to the page.
this.Controls.Add(table1);

Yes in that name space you will want to look at:
HtmlTable
HtmlTableRow
HtmlTableCell
Use is like so:
HtmlTable htmlTable = new HtmlTable(); //creating table object
HtmlTableRow htmlRow = new HtmlTableRow(); //creating row object
HtmlTableCell htmlTableCell = new HtmlTableCell(); //create cell
htmlTableCell.InnerHtml = "<b>Test Text Bolded</b>";//setting cell content
HtmlTableCell textTableCell = new HtmlTableCell(); //create cell
textTableCell.InnerText = "Test plain text"; //setting cell content
htmlRow.Cells.Add(htmlTableCell); //add cell to row
htmlRow.Cells.Add(textTableCell); //add cell to row
htmlTable.Rows.Add(htmlRow); // add row to table

HtmlTableCell = new HtmlTableCell("th");

Related

Populate Table with Table Rows and Cells on LinkButton click event while getting info from XML

Basically I create Rows then Cells then i populate the Cells with LinkButton which is getting Text value from XML nodes .
Code :
private void linkBTN_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string text = btn.Text.ToString();
XmlDocument clickDoc = new XmlDocument();
clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
int count = btnNode.Count;
foreach (XmlNode node in btnNode)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
row.Cells.Add(cell);
MainTable.Rows.Add(row);
}
}
My problem is that i want to create 4 cells for each row and my code creates 1 cell for each row untill the Nodes end
You need another loop to add more columns to each row
foreach (XmlNode node in btnNode)
{
TableRow row = new TableRow();
for (int i = 0; i < 4; i++){
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
row.Cells.Add(cell);
}
MainTable.Rows.Add(row);
}

dynamic data binding gridview

my datatable formed as follows
DataTable dtFinalData = new DataTable();
//Adding columns for BR details
dtFinalData.Columns.Add("SupId", typeof(string));
dtFinalData.Columns.Add("SupName", typeof(string));
DateTime dt = DateTime.Today;
int num = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
//--> adding columns for date part (1-31)
for (int i = 1; i < num + 1; i++)
{
dtFinalData.Columns.Add(i.ToString(), typeof(bool));
}
//<-- adding columns for date part (1-31)
#endregion
#region Fill dtFinalData
//--> Looping each BR from tblBrList
foreach (DataRow BrRow in dtBrList.Rows)
{
DataRow dr = dtFinalData.NewRow();
int SupId = Convert.ToInt32(BrRow[0]); //retrieve BR ID from dtBrList
String supName = BrRow[1].ToString(); //retreive Supervisor name from dtBrList
dr["SupId"] = SupId.ToString();
dr["SupName"] = supName;
for (int i = 1; i < num + 1; i++)
{
DateTime dtRunningDate = new DateTime(2013, 5, i);
//select BR_SUP_CODE,
DataRow[] drAttendance = dtAttendance.Select("BR_SUP_CODE=" + SupId + " AND SMS_DATE=#" + dtRunningDate + "#", string.Empty);
if (drAttendance.Length == 1)
{
//CheckBox chkbx = new CheckBox();
//chkbx.Checked = true;
dr[i.ToString()] = true;
}
else
{
//CheckBox chkbx = new CheckBox();
//chkbx.Checked = false;
dr[i.ToString()] = false;
}
}
dtFinalData.Rows.Add(dr);
}
//<-- Looping each BR from tblBrList
#endregion
GridView1.DataSource = dtFinalData;
GridView1.DataBind();
Now i want to add checked image in place of true and unchecked image in place of false.how to bind grid view dynamically such that in place of disable check box i want to insert two types of image?
Your DataTable part is fine and continue to add the True/False text as per the logic. Now you should handle the GridView part. So, define an event handler for the OnRowDataBound event of GridView.
In this event only, check for the Text property of the cells, and if True/False, clear the cells and add the required image.
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" ... />
And your event handler will have code as below:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Image chkImage = new Image();
chkImage.ImageUrl="~/images/Checked.png";
Image UnchkImage = new Image();
UnchkImage.ImageUrl = "~/images/UnChecked.png";
if (e.Row.RowType == DataControlRowType.DataRow)
{
// We will start from 3rd cell because the first 2 cells are
// for SupID & SupName, where we don't need to place images
for (int cellIndex = 2; cellIndex < GridView1.Columns.Count; cells++)
{
// Add Checked Image when cell text is true
if (e.Row.Cells[cellIndex].Text == "true")
{
// clear the cell and add only the image
e.Row.Cells[cellIndex].Controls.Clear();
e.Row.Cells[cellIndex].Controls.Add(chkImage);
}
// Add Unchecked Image when cell text is false
if (e.Row.Cells[cellIndex].Text == "false")
{
// clear the cell and add only the image
e.Row.Cells[cellIndex].Controls.Clear();
e.Row.Cells[cellIndex].Controls.Add(unchkImage);
}
}
}
}

Odd Numbered Cell Not Added To Pdf

I am trying to add PdfPCell inside a loop to a iTextSharp Table with 2 columns in a Document. But if the count inside the loop is an odd number. Then the last cell does not get added. Can someone please provide a solution to this problem?
My code is below:
var doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~/QrCodes/") + fileName + ".pdf", FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 100;
foreach (var item in items)
{
if (itemImages.Any(p => p.Reference == item.Reference) == true)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(#item.ItemQrCode));
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, ImageFormat.Jpeg);
PdfPCell cellImage = new PdfPCell(pdfImage);
cellImage.HorizontalAlignment = Element.ALIGN_CENTER;
cellImage.VerticalAlignment = Element.ALIGN_MIDDLE;
cellImage.Border = 0;
table.AddCell(cellImage);
}
}
doc.Add(table);
doc.Close();
On your PdfPTable you can call the CompleteRow() method when you're done and missing cells will be filled in.

Add second header row to GridView BELOW already existing header row

I'm trying to dynamically insert a 2nd header row at a GridView's OnRowCreated event. However, I can't seem to get the row to be inserted anywhere besides the first spot in the gridview's row index. The code below fails on the last line, where the header is actually added to the grid. Index out of bounds exception. How can I add this header row below the already existing header row? Help is much appreciated, thanks!
protected void gvwProd_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView gvw = (GridView)sender;
GridViewRow HeaderRow = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Prod Comments - TS/LID";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 4;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Materials Comments - TS/LD";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 8;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Plant Comments - TS/LID";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 11;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
gvw.Controls[0].Controls.AddAt(1, HeaderRow);
}
}
Had a similar problem. You could try casting the GridView to a Table and access the .Rows method:
((Table)gvw.Controls[0]).Rows.AddAt(1, HeaderRow);
source

Get Id of dynamically added control

i have one html table "table "and added controls it at run time as :
HtmlTableRow tabelrow = new HtmlTableRow();
HtmlTableCell tablecell = new HtmlTableCell();
Label lbl = new Label();
tablecell.Controls.Add(lbl);
then i want to get each controls by using foreach
such as (foreach control c in table.controls) or (foreach control c in tablecell.controls)
but its not working.
Tried the following. I can get the controls with the ID but you need to assign an ID to it first.
HtmlTable table = new HtmlTable();
for (int i = 0; i < 10; i++)
{
HtmlTableRow tablerow = new HtmlTableRow();
tablerow.ID = Guid.NewGuid().ToString();
HtmlTableCell tablecell = new HtmlTableCell();
tablecell.ID = Guid.NewGuid().ToString();
Label lbl = new Label();
lbl.ID = Guid.NewGuid().ToString();
tablecell.Controls.Add(lbl);
tablerow.Controls.Add(tablecell);
table.Controls.Add(tablerow);
}
List<string>RowID = new List<string>();
List<string>CellID = new List<string>();
List<string>LabelID = new List<string>();
foreach (Control Row in table.Controls)
{
//Each control will be a tablerow.
RowID.Add(Row.ID);
CellID.Add(Row.Controls[0].ID);
LabelID.Add(Row.Controls[0].Controls[0].ID);
}
What exactly are you trying to do? Please elaborate.

Resources