Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:44:44+00:00 2026-05-22T19:44:44+00:00

I have a page where I create div controls dynamiclly and number them automaticlly.

  • 0

I have a page where I create div controls dynamiclly and number them automaticlly.

subCell = new TableCell();
subCell.Controls.Add(new LiteralControl(
    "<div id=\"picker" + Index.ToString() + "\" runat=\"server\" 
     class=\"colorSelector\"><div style=\"background-color: #000000;\">Text
     </div></div>"));
subRow.Cells.Add(subCell);
subTb.Rows.Add(subRow);

Later in the code I want to get the background-color value like so:

HtmlGenericControl div;

div = (HtmlGenericControl)Page.FindControl("picker" + e.CommandArgument.ToString());

string colorCode = div.Style["background-color"].ToString();

after these line of code I get a null object ref error.
div is null.
I have tried HtmlControl and LiteralControl as the object type and that does not help either.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:44:45+00:00Added an answer on May 22, 2026 at 7:44 pm

    Page.FindControl only works for server controls. You are assigning an id to text within the literal control which happens to be a div, but not the control itself. If you set the id of the control you should be able to find it, but I don’t know if that’s what you’re intending.

    In response to your comment, check your HTML. The runat=server will probably be there because ASP.NET isn’t processing it as a control, it is treating it as content. Try something like this, noting that Page.FindControl only works on immediate children. Here I’ve declared a server panel named ‘declaredPanel’ in the aspx. ClientIdMode.Static makes it so ASP.NET won’t add parent names to the control (like “MainContent_childPanel”)

    <asp:Panel ID="declaredPanel" runat="server" ClientIDMode="Static" />
    

    In Page_Load:

        Panel p = new Panel();
        p.Style["background-color"] = "#aaeeaa";
        p.ID = "childPanel";
        p.ClientIDMode = System.Web.UI.ClientIDMode.Static;
        p.Controls.Add(new LiteralControl("<div id=\"div111\" runat=\"server\">Hello, world!</div>"));
        declaredPanel.Controls.Add(p);
        Panel p2 = declaredPanel.FindControl("childPanel") as Panel;
        string colorCode = p2.Style["background-color"]; // reports "#aaeeaa"
    

    Produces this:

    <div id="declaredPanel">
        <div id="childPanel" style="background-color:#aaeeaa;">
            <div id="div111" runat="server">Hello, world!</div>
        </div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a login page. I want to create ASP.NET TextBox controls that have
i have to create one asp.net mvc page that will show template of controls.
I have my views render to route requests like /controller/action (for instance, /Page/Create), so
I have a page which basically allows an admin user to create manager user
I have set permission to allow anonymous user to create page. but when the
i have to create an asp.net page dynamically on runtime. It should work like
I have few question in this regard When you create an internet page, does
I have a multiview control on my page and a menu to create a
I have looked on FaceBook Developer page and found that it's possible to create
I'm trying to create a simple portfolio page. I have a list of thumbs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.