简体   繁体   English

C#代码中的Div样式

[英]Div style in C# code

Is it possible to put div styles in a C# label control? 是否可以将div样式放入C#标签控件中? Working with iTextSharp. 使用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> 我的<br/>标记有效,但<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. 我认为在这种情况下最好添加asp:Literal so the default asp styling for the Label wont be a problem. 因此,Label的默认ASP样式不会有问题。

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 . 您可以将标签添加到某些控件中(根据需要签出'literal'标签),但是,使用CssClass属性添加类可能会更好。

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. 您将<div>放入标签中,就语义而言,这是一个坏主意。 What I'd do, personally, is make a Panel tag, which renders a <div > anyway. 我个人要做的是制作一个Panel标签,无论如何都会渲染一个<div >。

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) (Java)

<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. asp:Label渲染一个跨度,它是一个内联标签。 So you can't put a div in a span. 因此,您不能将div放在跨度中。 You can replace your Label by a simple div with runat server and put your text with InnerHtml or InnerText attribute 您可以使用runat服务器将其Label替换为简单的div,并使用InnerHtml或InnerText属性放置文本

aspx ASPX

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

aspx.cs aspx.cs

myDiv.InnerHtml = "..."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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