0

I am using the following code to make an html div in c#

System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
        new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
            dynDiv.ID = "dynDivCode";
            dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
            dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
            dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
            dynDiv.InnerHtml = "I was created using Code Behind";

            this.Controls.Add(dynDiv);

But this doesnot do a thing, infact it gives an error at the last line that dynDiv is not a valid argument. I want to use div here to simulate cache memory line and placement of words in cache memory.Please tell me how to do it

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Hanya Idrees
  • 453
  • 1
  • 8
  • 24
  • In your comments you say you are using WinForms, not ASP. You can't combine HTML or ASP.Net controls with WinForms. You can use a Panel. – JBrooks Dec 29 '13 at 01:37

4 Answers4

4

You could just embed the html inside a literal control.

this.Controls.Add(new LiteralControl("<div style='color: gray; height: 20px; width: 300px;'>I was created using Code Behind</div>"));
scartag
  • 17,548
  • 3
  • 48
  • 52
0

use LITERAL CONTROL

HTML (example)

<asp:Panel ID="panel" runat="server"> </asp:Panel>

C#

//And create the style as you want

panel.Controls.Add(new LiteralControl("<div style='color: gray; height: 20px; width: 300px;'>I was created using Code Behind</div>"));
Severiano
  • 1,083
  • 3
  • 25
  • 54
  • this.Controls.Add(new LiteralControl("")); i have used this syntax you provided, but this is giving an error that invalid argument. Is there a namespace requird for using this control – Hanya Idrees Apr 01 '13 at 16:08
  • i am using a split container, on whose i add the control literalcontrol but this also gives the same error splitContainer1.Panel2.Controls.Add(new LiteralControl("
    "));
    – Hanya Idrees Apr 01 '13 at 16:32
  • so what you want to use is not this. You can't put divs in split containers, this code we gave is for asp.net. – Severiano Apr 01 '13 at 16:35
  • maybe what you want to use is a panel not a div – Severiano Apr 01 '13 at 16:35
0

maybe this helps: Adding panels to SplitContainer in Windows Forms

    Panel panel = new Panel();
    Label lbl = new Label();
    public Form1()
    {
        InitializeComponent(); 
        panel.BackColor = Color.Gray;
        panel.Height = 20;
        panel.Width = 300;
        lbl.Text = "I was created using Code Behind";
        panel.Controls.Add(lbl);

        dynDiv.Panel1.Controls.Add(panel);
    }

for what you say this is what you want

Community
  • 1
  • 1
Severiano
  • 1,083
  • 3
  • 25
  • 54
0

I made as Diogo Severiano said and it worked.

In my case I needed to add controls to a div and I could not use a literal control because intellisense marks: "controls cannot be added to a literal control".

If that is the case do as he did.

Label lblParaname = new Label(); lblParaname.Text = Record.Parameter_Name + ": "; lblParaname.ID = "lbl" + Record.Parameter_Name; lblParaname.EnableViewState = true; lblParaname.Attributes.Add("cssclass", "grid_3 alpha omega"); Panel pLblContainer = new Panel(); pLblContainer.CssClass = "grid_3"; pLblContainer.Controls.Add(lblParaname); pSubsPar.Controls.Add(pLblContainer);