Adding space BETWEEN cells in TableCell() - c#

My code is below. I want to add space between cells in tableCell()
something the equivalent of
<td></td> <td></td>
I am NOT looking for cellpadding or cellspacing because they both add space between the left cell border and the left table border (wall); I want to add space between two CELLS, not the table and the cell;
protected void Page_Init(object sender, EventArgs e)
{
Table tb = new Table();
tb.ID = "tb1";
for (int i = 0; i < iCounter; i++)
{
TableRow tr = new TableRow();
TextBox tbox = new TextBox();
tbox.ID = i.ToString();
TableCell tc = new TableCell();
tc.Controls.Add(tbox);
tr.Cells.Add(tc);
tb.Rows.Add(tr);
}
pnlScheduler.Controls.Add(tb);
}
Any ideas?

Try this:
TableCell tc = new TableCell();
HtmlGenericControl div = new HtmlGenericControl("div");
div.Style.Add("padding-left", "5px");
div.Style.Add("padding-right", "5px");
div.Controls.Add(tbox);
tc.Controls.Add(div);
tr.Cells.Add(tc);
tb.Rows.Add(tr);

An alternative might be this (I modified your code and added a label, which you can add alternate to every "tc.Controls.Add(tbox);"
protected void Page_Init(object sender, EventArgs e)
{
Table tb = new Table();
tb.ID = "tb1";
for (int i = 0; i < iCounter; i++)
{
TableRow tr = new TableRow();
TextBox tbox = new TextBox();
TextBox tLabel = new Label();
tbox.ID = i.ToString();
tLabel.width = 10;
TableCell tc = new TableCell();
tc.Controls.Add(tbox);
tc.Controls.Add(tLabel);
tc.Controls.Add(tbox);
tc.Controls.Add(tLabel);
tc.Controls.Add(tbox);
tc.Controls.Add(tLabel);
tr.Cells.Add(tc);
tb.Rows.Add(tr);
}
pnlScheduler.Controls.Add(tb);
}

*edit
My apologies for the margin, I don't think it is possible with margins, however I was able to create a css that can be applied to the middle cells give them padding on both the left and right sides.
but to get around the fact that the first and last elements don't push out against the wall of the table you have to apply the class to only the middle td's
example of css code
table
{
border:1px solid black;
border-collapse: collapse;
table-layout: fixed;
}
td.middle
{
border:1px solid black;
padding-left: 20px;
padding-right: 20px;
}
and then your middle td's would be created with the <td class="middle"></td>
however to convert this type of code to c# I'm not entirely sure. I will keep looking on how to apply this class to only specific elements, but your best bet would be to create a flag that checks the element to make sure that they aren't the first or last ones in the table, unless you are adding them manually then you just add the class manually as needed.
However might I suggest that if this is for page layout, your better off having css do the work for you instead of using table design. But if it's to show data in a table format then never mind, I said nothing.
Hope this helps. will post if I find anything else.

you could always add "empty" cells of the desired with.

For giving space between the table cells we have to use table's cellpadding and cellspacing property.
<table cellspacing="1" cellpadding="2">
</table>
<TABLE CELLSPACING="X">
The X in the line, is replaced with the number of pixels you want between the cells.
<TABLE CELLPADDING="X">
The X in the line, is replaced with the number of pixels you want between the text and cell edge.

When you're ready to add two cells with this spacing, create a table instead; with one row and two td's like:
<table cellspacing="2"><tr><td>stuff</td><td>more stuff></td></tr></table>

Apply a left margin to the table's td (except the last one in each row).
You can do it by assigning a common class, or by applying a class that reset the margin only to the last td.
You can use jQuery either and inject margin, but doing it via css only is cleaner.

Related

C# adding empty literal in empty tablecell results in

I'm reading a menu control code. The code uses a tablerow in a table to construct the menu. After each menu item tablecell, it adds the code:
TableCell td = new TableCell();
Literal lt = new Literal();
td.Controls.Add(lt);
tr.Cells.Add(td);
And it results in a space between each menu item, when displaying the menu table. I just wonder why it does that? Does an empty tablecell take space?
I've noticed .NET adds in into empty cells, this has to be down to HTML tables getting a bit ugly looking with an empty cell; it won't get any border and can sometimes (depending on your styling) look rather weird. If you're wanting a gap between table cells use cellspacing or apply a style with margin-right.
On a side note it's recommended you be using var on the left side of your declarations (only when the right side clearly states what it is), ie:
var td = new TableCell();
var lt = new Literal();
td.Controls.Add(lt);
tr.Cells.Add(td);
You would not use it like this:
var x = GetMyVariable();

Add a em tag inside table header cell dynamically in Asp.net

I have a table in my code. The table headers are decide dynamically
The heder is for textbox column.
var thc = new TableHeaderCell { Text = "Iteration", CssClass = "iteration" }
I have to change the logic of how the label should be displayed to indicate the textbox entry is mandatory or not.
<em id="lblIterationMandatory" runat="server" class="mandatoryIndicator"
style="display: none;">*</em>
I want to add a * star to the headercell if the text box value is mandatory whose css is defined mandatory Indicator class.
My output should be Iteration(black) *(red color,bold).
var thc= new TableHeaderCell { Text = #"Iteration <em id='Iteration' runat='server' class='mandatoryIndicator' style='display: none;'>*</em>", CssClass = "iteration" };
I tried this way. But * will not be dsiplayed.
Since both Iteration and * are different color I think i have to use a separte control for *.
User InnerHtml instead of Text
var thc= new TableHeaderCell { InnerHtml= #"Iteration <em id='Iteration' runat='server' class='mandatoryIndicator' style='display: none;'>*</em>", CssClass = "iteration" };
also using style='display: none;' will hide any thing on the browser on which it is written.
You set display of em to none change it to block
var thc= new TableHeaderCell { Text = #"Iteration <em id='Iteration' runat='server' class='mandatoryIndicator' style='display: block;'>*</em>", CssClass = "iteration" };

HtmlTableCellCollection.Add() needs NEW Cell instance everytime?

ContentPlaceHolder wrapperCPH = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
HtmlTable tblCumulativeCredits = (HtmlTable)wrapperCPH.FindControl("tblCumulativeCredits");
//If your table is not inside of a ContentPlaceHolder, you can just test with "this.FindControl("yourHTMLTableID");
HtmlTableRow tableRow = new HtmlTableRow();
HtmlTableCell tableCellAmount = new HtmlTableCell();
HtmlTableCell tableCellCurrency = new HtmlTableCell();
tableCellAmount.InnerText = item.amount.ToString();
tableCellCurrency.InnerText = item.currencyCode.code;
tableRow.Cells.Add(tableCellAmount);
tableRow.Cells.Add(tableCellCurrency);
tableRow.Cells.Add(tableCellCurrency);
tblCumulativeCredits.Rows.Add(tableRow);
HTML:
<table id="tblCumulativeCredits" runat="server">
<tr>
<th class="th">Amount</th>
<th class="th">Currency1</th>
<th class="th">Currency2</th>
</tr>
</table>
Now imagine I'm adding rows using this code sample in a loop and I know that my Currency1 = Currency2 in some case. So I'm using same Cell object as the above example. But there goes something interesting, if I add same currencies like the example; it only adds the first one, the third cell (Currency3) is empty.
It only adds the second currency if I create a new Cell instance, and add it:
tableRow.Cells.Add(tableCellAmount);
tableRow.Cells.Add(tableCellCurrency1);
tableRow.Cells.Add(tableCellCurrency2);
I think this is the first time I saw that an "Add()" method is working this way. Shouldnt it be creating a new instance of the given parameter (cell object) under the hood? I'd appreciate if anyone can explain what is the logic behind this? Thank you
I think it has nothing to do with the implementation af the "Add" method, instead I think this happens during the rendering.
Just remember that you are adding the same instance two times and that not only means the value of the cell (maybe also some kind of reference that exmplain the "overwriting" behavior).
So, you better create a new instance.
Cheers

Add <thead> tags to Table using System.Web.UI.WebControls

I need to add <thead> tags to my loop somehow. The tableHEaderRow class only uses <tr><th> tags, and I need them nested in a <thead> tag for styling purposes.
I have tried using a variety of methods including string writer, and literal controls but I can't seem to get it to work.
TableHeaderRow thr = new TableHeaderRow();
tbl.Controls.Add(new LiteralControl("<thead>")); // This is the line I need to add
foreach (DataColumn col in dt.Columns)
{
TableHeaderCell th = new TableHeaderCell();
th.Text = col.Caption;
thr.Controls.Add(th);
}
tbl.Controls.Add(new LiteralControl("</thead>")); // This is the line I need to add
tbl.Controls.Add(thr);
...
...
tbl.RenderControl(w);
tableString = sw.ToString();
// Then my aspx page displays my tableString variable.
What other ways Can I try to add the <thead> tags into my loop manually?
I am using System.Web.UI.WebControls.
You set a TableRow control as being in a <thead> with the TableSelection property.
For example: <asp:TableRow TableSection="TableHeader"/> will generate a TableRow control in the <thead> section of the table.
Dynamically, you can also set rowControl.TableSection = TableRowSection.TableHeader;
The three options are TableHeader, TableBody and TableFooter.

Last column of a table in PDF (iTextSharp) not justified

I generated a PDF file starting from an HTML page using iTextSharp.
But tables are rendered with the last column not justified, as you can see in the image.
What can I do to fix this?
Here's part of the code:
StyleSheet ss = new StyleSheet();
TextReader htmlReader = new StringReader(strHTML);
ArrayList htmlarraylist = HTMLWorker.ParseToList(htmlReader, ss);
foreach (IElement el in htmlarraylist)
{
document.Add(el);
}
In strHTML there is this HTML code:
<TABLE style="WIDTH: 100%" border=1 cellSpacing=1 cellPadding=1><BR><TBODY><BR><TR><BR><TD>1</TD><BR><TD>2</TD><BR><TD>4</TD><BR><‌​TD>4</TD><BR><TD>5</TD></TR><BR><TR><BR><TD>6</TD><BR><TD>7</TD><BR><TD>8</TD><BR‌​><TD>9</TD><BR><TD>0</TD></TR><BR><TR><BR><TD>0</TD><BR><TD>98</TD><BR><TD>7</TD>‌​<BR><TD>5</TD><BR><TD>4</TD></TR><BR><TR><BR><TD>3</TD><BR><TD>2</TD><BR><TD>3</T‌​D><BR><TD>4</TD><BR><TD>5</TD></TR><BR><TR><BR><TD>76</TD><BR><TD>7</TD><BR><TD>9‌​</TD><BR><TD>0</TD><BR><TD>4</TD></TR></TBODY></TABLE>
Ciao
do you use a loop to produce your pdf,? are you sure for each cell you make the appropriate aactions...
int howManyCell = ....
for (int i = 0; i < howManyCell + 1; i++) {
PdfPCell cell = new PdfPCell();
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
cell.VerticalAlignment = Element.ALIGN_TOP;
table.AddCell(cell);
}
Maybe do you use an automatic transformation, without any use of pdf class elements ? In thi case show us the header of your html table and a sample of the first table row, to check out if that is normal
I found the problem!
All those BR tags were messing up my tables! Those were added by a replace hided in another class:
Replace("\r\n", "<BR>");
Thank you guys for your help!

Categories

Resources