C#代码中的Div样式

时间:2013-04-22 10:18:22

标签: c#

是否可以将div样式放在C#标签控件中?使用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>"
};

我的<br/>代码有效,但不是<div>

整个代码:

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

4 个答案:

答案 0 :(得分:1)

我认为在这种情况下添加asp:Literal会更好。所以Label的默认asp样式不会有问题。

答案 1 :(得分:0)

您可以为某些控件添加标签(请根据需要查看“文字”标签),但是,您可能会发现使用CssClass property添加课程会更好。

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

答案 2 :(得分:0)

你在标签中加了<div>,这在语义方面是个坏主意。我个人做的是制作一个Panel代码,该代码会生成一个<div&gt;反正。

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.

而且,这取决于你如何做,但在代码可维护性和设计方面,我会这样做:

(VB)

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

然后,在你的代码beind:

DisplayLabelBackground.Text = LabelBackground.Text;

答案 3 :(得分:0)

asp:Label渲染一个内联标签。所以你不能把一个div放在一个范围内。你可以用一个带有runat服务器的简单div替换你的Label,并将你的文本放在InnerHtml或InnerText属性

ASPX

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

aspx.cs

myDiv.InnerHtml = "..."