0

I'm trying to add a span, inside an anchor, inside a dd tag. for some reason this:

protected Control MakeDD() {
    var dd = new HtmlGenericControl("dd");
    var link = new HtmlGenericControl("a");
    var span = new HtmlGenericControl("span");

    link.Controls.Add(span);
    dd.Controls.Add(link);
    return dd;
}

only generates

<dd><a></a></dd>

instead of

<dd><a><span></span></a></dd>

how do I add controls to a control, then add that control to another control?

3
  • 1
    Yo dawg, I heard you like controls so we put a control in your control... Commented Jun 4, 2011 at 0:39
  • I dropped your code into a webform and got your expected result. Commented Jun 4, 2011 at 1:00
  • I believe the issue was that one of the controls I was attempting to add was actually writing a string instead of creating a control. Someone's custom implementation.. Anywho, thanks for the help Commented Jun 6, 2011 at 17:33

2 Answers 2

1

Dropping a panel on a page as the container and doing:

        protected void Page_Load(object sender, EventArgs e)
        {
            pnlTest.Controls.Add(MakeDD());
        }

emits the following on the page:

<div id="MainContent_pnlTest">

    <dd><a><span></span></a></dd>
</div>

This is asp.net 4

Sign up to request clarification or add additional context in comments.

Comments

1

What happens if you set something to the span value, does it show up then?

...
var span = new HtmlGenericControl("span");
span.InnerHtml = "test";
link.Controls.Add(span);
...

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.