How to align cell vertically with RowSpan>1 - asp.net

I want to have this ,
but instead have this .
Please note, when I change VerticalAlign.Middle to VerticalAlign.Top it actually works as expected.
Please see below code that I am trying:
// I assume that table table is already created and well-defined
// Create a new row
TableRow tRow = new TableRow();
tRow.HorizontalAlign = HorizontalAlign.Center;
// add the row to the table
table.Rows.Add(tRow);
// Create a new cell
TableCell tCell = new TableCell();
tCell.VerticalAlign = VerticalAlign.Middle; // Want to get it in the middle of two merged rows
tCell.RowSpan = 2;
tCell.Text = "England";
tCell.Font.Bold = true;
tRow.Cells.Add(tCell);
// Create new cell
tCell = new TableCell();
tCell.Text = "2010";
tCell.Font.Bold = true;
tRow.Cells.Add(tCell);
// Create new row
tRow = new TableRow();
// add the row to the table
table.Rows.Add(tRow);
// Create new cell
tCell = new TableCell();
tCell.Text = "2011";
tCell.Font.Bold = true;
tRow.Cells.Add(tCell);
Update: Please see extra code below. I don't have html code as such, but I looked at sw.ToString() and formatting looks right but still excel file does not seem to be rightly formatted. My browser is IE but I think it does not matter.
I tried tCell.CssClass = "className"; result is the same.
public static void Comparison_Report(string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a table to contain the grid
Table table = new Table();
///----- Original code goes here----
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}

Finally i've tested it by myself. It seems not to work in Excel if you use valign="middle" on your table-cell. You must use CSS.
So instead of
tCell.VerticalAlign = VerticalAlign.Middle
do
tCell.Style.Add("vertical-align", "middle")
That will align the text correctly in excel. Excel itself uses css-classes if you export it from excel to html.
But repeating my comment, i would recommend to generate a real excel file instead of creating html that might be openened and interpreted correctly.
It's very simple with EPPlus: Exporting data to excel

Do this in CSS please!! Set a class on the cell that you want to have 'England' in it and target that class.
Code
tCell.Text = "England";
tCell.CssClass = "className";
Css
td.className {
vertical-align:middle;
}
Edit
Ok so by popular demand an explanation for why to use CSS here rather than set this in the creation of the table cell.
What you gain from using a seperate stylesheet is a whole heap of power and control on how this element is going to look on the client. When you set this in code you are explicitly saying it should only be like this - however when you set it in CSS and use a stylesheet you can do things like target different platforms, change the position easily, add extra elements etc. This is the old discussion between having inline style and pulling the style out into a separate sheet. I think this topic is quite well discussed on the web if you care to read more about it...
Edit 2
I just tried your code as you pasted it and it worked fine for me - the cell was middle aligned. I would use Firebug to try figure out if there are some other styles that are acting on that table cell to make it have this behavior.

<td align='left' valign='middle' ></td>
Not working in exporting excel file from code behind.so i search in internet and found this example.
td.className {
vertical-align:middle;
}
this save my time.
Thanks

Related

How can I clear a datatable in asp listview?

When I use the code below, I remove the datatable values, but the data table structure still exists and displays empty fields (see pics) with the DOM explorer showing an empty table and table rows.
How can I clear the datatable values and the table itself? This way when I repopulate search again, the empty smaller table isn't present?
lvwOutput.Items.Clear();
lvwOutput.DataSource = null;
lvwOutput.DataBind();
Before
After items.clear and datasource = null
This is ridiculous and I believe there is a better way to do this, but the never ending server/client battle makes this harder than it should be. My listview binded to a datatable is called lvwOutput.
In my btnClear I had to put the following. You cannot hide the element or clear the items in the server side asp code for this to work
ScriptManager.RegisterStartupScript(Page, GetType(), "emptyTable", "javascript:emptyTableRows(); ", true);
In my javascript code I had to put the following, this clears the client code
function emptyTableRows(){
var tableHeaderRowCount = 0;
var table = document.getElementById('lvwOutputTable');
var rowCount = table.rows.length;
for (var i = tableHeaderRowCount; i < rowCount; i++) {
table.deleteRow(tableHeaderRowCount);
}
}
And then in the portion of my code that would display the listview and datatable when the user initiates another sql search. This clears the server side.
lvwOutput.Items.Clear();
lvwOutput.DataSource = null;
lvwOutput.DataBind();
You can create a property the stores the data table in session that way you can access it during the click event.
DataTable dtbleDataSource
{
get
{
return Session["dataSource"] as DataTable
}
set
{
Session["dataSource"] = value;
}
}
In your click event you can say:
dtbleDataSource.Reset();

Image won't display when using session in .NET

I am working with a session and I want to display image when choosing it from a gridview, so what I have two gridviews the first contain the rows from the database the second should contain the rows chosen from the first one the issue that I have is the first display image with no problem but the second won't show up any.
This is my code for the first one
foreach (CONTENT c in ls)
{
string ext = Helper_GetExtensionFromMimeType(c.MimeType);
c.TmpFilename = string.Format("~/Images/Contents/Content-{0}{1}", c.ContentID, ext);
}
gridview1.DataSource = ls;
gridview1.DataBind();
The code for the second one is (the session name is panier)
CONTENT dummy;
dummy = new CONTENT();
dummy.TmpFilename = Server.MapPath(string.Format("~/Images/Contents/Content-{0}{1}",
c.ContentID, ext));
panier.Add(dummy);
gridview2.DataSource = panier;
gridview2.DataBind();
It will be hard to determine what the issue exactly is without seeing your view code.
Anyway, make sure the image indeed exists in the generated location.
Place a breakpoint after the line dummy.TmpFilename and ensure the link is indeed formatted with the correct c.ContentID and ext, then copy the link to the browser making sure the image exists.
i found a solution to the probleme and here it is if anyone is interested
i replace
dummy.TmpFilename = Server.MapPath(string.Format("~/Images/Contents/Content-{0}{1}",c.ContentID, ext));
with
dummy.TmpFilename =string.Format("~/Images/Contents/Content-{0}{1}",c.ContentID, ext);

iTextSharp - MVC / HTMLWorker one string to add to a paragraph

I am sending the contents of Telerik MVC Editor to the controller using Ajax as a string:
I comes out as:
"<strong>Hello world!</strong> <object height=\"1\" id=\"plugin0\" style=\"position:absolute;z-index:1000;\" type=\"application/x-dgnria\" width=\"1\"><param name=\"tabId\" value=\"{84594B7B-865F-4AD7-A798-294A8B0EB376}\" /></object>"
In the controller, I save the string to a session variable using the following:
string comments = HttpUtility.HtmlDecode(Text);
MySession.Current.pdfText = comments;
I can convert it to PDF using
.....
HTMLWorker parser = new HTMLWorker(document);
.......
However I could not add any other paragraphes to the same page, it makes a new page.
I tried to use the following to make a new paragraph using HTMLWORK:
string PDFText = MySession.Current.pdfText;
string PDFText1 = HTMLWorker.Parse(PDFText);
StringReader reader = new StringReader(PDFText1);
paragraph.Add(reader);
I got these errors:
cannot convert from 'System.IO.StringReader' to 'string', and
The best overloaded method match for 'iTextSharp.text.html.simpleparser.HTMLWorker.Parse(System.IO.TextReader)' has some invalid arguments, and
The best overloaded method match for 'iTextSharp.text.Phrase.Add(string)' has some invalid arguments
I would appreciate your suggestions, thanks in advance.
I have worked a lot on iTextSharp and I usually follow the approach of creating a table or nested tables; if needed, with multiple rows and columns and using colspan to spread out my content on the page.
PdfPTable table = new PdfPTable(1); //Create a new table with one column
PdfPCell cell = new PdfPCell(); //Create an empty cell
StyleSheet style = new StyleSheet(); //Declare a stylesheet
style.LoadTagStyle("h1", "color", "red"); //Create styles for your html tags which you think will be there in PDFText
ArrayList objects = HTMLWorker.ParseToList(new StringReader(PDFText),style); //This transforms your HTML to a list of PDF compatible objects
for (int k = 0; k < objects.Count; ++k)
{
cell.AddElement((IElement)objects[k]); //Add these objects to cell one by one
}
table.AddCell(cell); //Add cell to table
document.add(table) //Add table to the document
try it out with just the paragraph once as you were trying in the question. otherwise this approach will definitely not result in a new page.
EDIT:
please see the updated code
PdfPTable table = new PdfPTable(2); //Create a new table with one column
PdfPCell cellLeft = new PdfPCell(); //Create an empty cell
StyleSheet style = new StyleSheet(); //Declare a stylesheet
style.LoadTagStyle("h1", "color", "red"); //Create styles for your html tags which you think will be there in PDFText
List<IElement> objects = HTMLWorker.ParseToList(new StringReader(PDFText),style); //This transforms your HTML to a list of PDF compatible objects
for (int k = 0; k < objects.Count; ++k)
{
cellLeft.AddElement((IElement)objects[k]); //Add these objects to cell one by one
}
table.AddCell(cellLeft); //Add cell to table
string url = "http://localhost:1713/PDF/Images/sample.jpg"; //Image Path(can give local machine path for testing)
PdfPCell cellRight = new PdfPCell();
Image jpg = Image.GetInstance(new Uri(url));
cellRight.AddElement(jpg);
table.AddCell(cellRight);
document.add(table);
Please google for padding, margins, borders and coloring etc..

Dynamically creating ASP.NET form controls

I have a form which, based on the answers given in the prior page, can have about 10 different variations in the combination of fields (most are the same, but several change). I decided rather than making 10 separate pages, I would try to make it dynamic. Eventually this will pull the form setup from a database, but for now I'm just trying to get the dynamic part to work. The following code kinda works, but it's giving me a weird result.
private void AddTestControls()
{
var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);
var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);
newbox.ID = "SecondBox";
newbox.Text = "This is also dynamic";
newbox.CssClass = "step";
DynamicDiv1.Controls.Add(newbox);
}
I've stepped through it and all the properties are getting set correctly, but when the page finally renders, only the SecondBox control is visible. There is no trace of the FirstBox. If I change it so that SecondBox is its own object (newebox2 for example) then both are visible, but with how I was thinking that I would ultimately do the form from the database, this could complicate things. I don't understand why the textbox object has to be recreated in order to add it to the Div's collection of controls. Am I going about this all wrong, or just missing a step somewhere?
Your "SecondBox" are overwriting the "FirstBox" newbox since it's still holding a reference to it. Create a new TextBox for the second box:
var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);
var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);
// Create a new TextBox
var secondBox = new TextBox();
secondBox.ID = "SecondBox";
secondBox.Text = "This is also dynamic";
secondBox.CssClass = "step";
DynamicDiv1.Controls.Add(secondBox);
I'm not quite sure why this could complicate things, but what you could do is create a method for creating a textbox, if that's easier:
TextBox CreateTextBox(string id, string text, string cssClass)
{
var box = new TextBox();
box.ID = id;
box.Text = text;
box.CssClass = cssClass;
return box;
}
And then
var newBox = CreateTextBox("FirstBox", "This is dynamic", "stepHeader");
DynamicDiv1.Controls.Add(newBox);
What's how it suppose to work. newbox1 is a reference so after the first time it's added to DynamicDiv1, it's there and if you change its Text, then the Text will be changed. You may find this SO useful. This SO demostrates the same issue you are having.

How do you make GridView load images with a DataSet in ASP.NET 2.0?

I have a pictures table that has the following columns:
PICTURE_ID int IDENTITY(1000,1) NOT NULL,
CATEGORY_ID int NOT NULL,
IMGDATA image NOT NULL,
CAPTION1 text COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
MIME_TYPE nchar(20) NOT NULL DEFAULT ('image/jpeg'),
IMGTHDATA image NOT NULL
In my code-behind I have this:
string tableName = "CATPICS";
SqlConnection dbConnection = new SqlConnection(connStr);
SqlDataAdapter daCatPics = new SqlDataAdapter("SELECT TOP(3) * FROM CATEGORY_PICTURES", dbConnection);
DataSet dsPics = new DataSet();
daCatPics.Fill(dsPics, tableName);
gvCatPics.DataSource = dsPics;
gvCatPics.DataMember = tableName;
gvCatPics.DataBind();
On the markup I pretty much have:
<asp:GridView ID="gvCatPics" runat="server"></asp:GridView>
However when the code executes, it simply ignores the two image columns (IMGDATA and IMGTHDATA). For some reason it doesn't recognize that they are image columns. Does anyone know of the simplest way of having it render the image?
you can make a page that returns the image as a stream and reference that page from an Image column (or an IMG tag in a template column); see GridViewDisplayBlob.aspx
There is no real simple way to do this. Basically you're going to need to create a handler to display the images. Then in your grid you will create an img tag with the URL pointing to your image handler.
An example of this can be found here.
Update: Much better example of this here.

Resources