简体   繁体   中英

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

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

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

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.

ie

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM