krisal
krisal

Reputation: 631

Div style in C# code

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();
            }

Upvotes: 2

Views: 1375

Answers (4)

binard
binard

Reputation: 1784

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 = "..."

Upvotes: 0

Dave
Dave

Reputation: 8451

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";

Upvotes: 0

Chris Dixon
Chris Dixon

Reputation: 9167

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;

Upvotes: 0

HasanAboShally
HasanAboShally

Reputation: 18675

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

Upvotes: 1

Related Questions