1

I am programmatically trying to add a checkbox with a unique ID to a div:

        foreach (var item in tweetList)
        {
            Panel tweetPanel = new Panel();
            tweetPanel.BorderColor = System.Drawing.Color.Black;
            tweetPanel.BorderWidth = 1;
            tweetPanel.Attributes["style"] = "padding: 20px; margin 20px; width: 20%;"; 
            tweets.Controls.Add(tweetPanel);
            CheckBox tweetChecker = new CheckBox();
            tweetChecker.ID = "checkBox" + count;
            tweetPanel.Controls.Add(tweetChecker);
            tweetPanel.Controls.Add(new LiteralControl("<img src=\"" + item.profileImageUrl + "\">"));
            count++;
        }

in this div:

    <div id="tweets" runat="server">
    </div>

But when I run it it says:

Control 'checkBox0' of type 'CheckBox' must be placed inside a form tag with runat=server.

Why wouldn't a programmatically created check box not be considered server side? And how do I fix this

2
  • 2
    wrap div with a form runat server, asp.net needs all server side controls in a form Commented Feb 26, 2016 at 20:17
  • 2
    The check box is considered server-side. But you're not placing it inside a server-side form. Commented Feb 26, 2016 at 20:20

1 Answer 1

1

The controls need to be inside a form element so that they can be posted to the server when the form is submitted.

<form id="myForm" runat="server">
    <!-- your controls -->
    <div id="tweets" runat="server">
    </div>
</form>

You can only add controls outside of a form element if those controls do not cause a postback.

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

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.