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

1
  • 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. Commented Dec 29, 2013 at 1:37

4 Answers 4

3

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>"));
Sign up to request clarification or add additional context in comments.

6 Comments

Scartag i have used this code this.Controls.Add(new LiteralControl("")); but it gives an error, that invalid argument, isthere any namespace required for using this code
@HanyaIdrees No, there isn't any namespace to add .. LiteralControl is inside System.Web.UI namespace.
i am coding in C#, not in aspx
@HanyaIdrees If you are not using asp.net then you won't be working on a codebehind... that's why you are having issues .. in an asp.net codebehind the Controls property is provided.
then what should i do now... pleaseee
|
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>"));

4 Comments

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
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("<div></div>"));
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.
maybe what you want to use is a panel not a div
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.