2

Is it possible to put div styles in a C# label control? Working with iTextSharp.

var background = new Label
{
        Text = "<div style='margin-left: 40px;'>" + "<br/><b><u>" + LabelBackground.Text + "</u></b><br/>" + "<b>" + LabelDoB.Text + "</b>" + LabelDoBFromDb.Text +
                                                 "<br/>" + "<b>" + LabelPhone.Text + "</b>" + LabelPhoneFromDb.Text + "<br/>" + "<b>" + LabelEmail.Text + "</b>" +
                                                 LabelEmailFromDb.Text + "<br/>" + "<b>" + LabelPosition.Text + "</b>" +
                                                 LabelPositionFromDb.Text + "<br/>" + "</div>"
};

My <br/> tags works, but not the <div>

Whole code:

            //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
            Document doc = new Document(PageSize.A4, 10, 10, 42, 35);

            try
            {
                PdfWriter.GetInstance(doc, new FileStream("c:\\Test11." + DropDownListDownload.SelectedItem.Text, FileMode.Create));
                var sv = new StringWriter();
                doc.Open();//Open Document to write 

                var hTextWriter = new HtmlTextWriter(sv);

                var background = new Label
                                     {
                                         Text = "<div style='margin-left: 40px;'>" + "<br/><b><u>" + LabelBackground.Text + "</u></b><br/>" + "<b>" + LabelDoB.Text + "</b>" + LabelDoBFromDb.Text +
                                             "<br/>" + "<b>" + LabelPhone.Text + "</b>" + LabelPhoneFromDb.Text + "<br/>" + "<b>" + LabelEmail.Text + "</b>" +
                                             LabelEmailFromDb.Text + "<br/>" + "<b>" + LabelPosition.Text + "</b>" +
                                             LabelPositionFromDb.Text + "<br/>" + "</div>"
                                     };
                background.RenderControl(hTextWriter);

                //LANGUAGES
                string languages = string.Empty;
                var lbLanguages = new Label();
                foreach (var vLang in BulletedListLanguages.Items)
                {
                    languages += vLang + "<br/>";
                }

                lbLanguages.Text = "<br/><b><u>" + LabelLanguages.Text + "</u></b><br/>" + languages;
                lbLanguages.RenderControl(hTextWriter);

                String strHtml1 = sv.ToString();

                var hw = new HTMLWorker(doc);
                hw.Parse(new StringReader(strHtml1));
            }

            finally
            {
                doc.Close();
            }
3
  • 3
    ASP.NET or what do you want to do? Commented Apr 22, 2013 at 10:19
  • @bash.d, I'm rendering my information to a PDF document, all works fine but i don't really know how to put a style on it. Working with iTextSharp, want me to put my whole code inside? Commented Apr 22, 2013 at 10:21
  • Well, not my kind of stuff, but other people might help. SO, go ahead! Commented Apr 22, 2013 at 10:22

4 Answers 4

1

i think its better to add asp:Literal in this case. so the default asp styling for the Label wont be a problem.

Sign up to request clarification or add additional context in comments.

2 Comments

or use CssClass and apply class using normal css.
+1 for using "asp:literal" . This is easy to use and more logic
0

You're putting a <div> in a label, which is a bad idea in terms of semantics. What I'd do, personally, is make a Panel tag, which renders a <div> anyway.

i.e.

var background = new Panel();

var text = new Literal
{
        Text = "<br/><b><u>" + LabelBackground.Text + "</u></b><br/>" + "<b>" + LabelDoB.Text + "</b>" + LabelDoBFromDb.Text +
                                                 "<br/>" + "<b>" + LabelPhone.Text + "</b>" + LabelPhoneFromDb.Text + "<br/>" + "<b>" + LabelEmail.Text + "</b>" +
                                                 LabelEmailFromDb.Text + "<br/>" + "<b>" + LabelPosition.Text + "</b>" +
                                                 LabelPositionFromDb.Text + "<br/>"
};

background.Controls.Add(text);

background.Attributes["margin-left"] = "40px"; // Preferably, I'd add a class and do this plainly in CSS.

And, it's up to you how you do it, but in terms of code maintainability and design, I'd do it like this:

(aspx)

<div style="margin-left: 40px">
    <br/><b><u><asp:Literal runat="server" ID="DisplayLabelBackground" /></u></b> // etc, but I *definitely* wouldn't use <b> and <u> tags either
</div>

Then, in your code beind:

DisplayLabelBackground.Text = LabelBackground.Text;

Comments

0

You can add tags to some controls (check out the 'literal' tag for your needs), however, you may find it better to add the class using the CssClass property.

Label label = new Label();
label.CssClass="class";
label.Text = "My content";

Comments

0

asp:Label render a span which is an inline tag. So you can't put a div in a span. You can replace your Label by a simple div with runat server and put your text with InnerHtml or InnerText attribute

aspx

<div id="myDiv" runat="server" style="margin-left:40px"></div>

aspx.cs

myDiv.InnerHtml = "..."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.