NullReferenceException error, c#

Posted on 2014-08-21
Last Modified: 2014-09-01
This code generates a few buttons dynamically:
private void makeButtonFor(int id, string caption)
        Button b = new Button();
            var temp = "medButton" + id.ToString();
            b.ID = temp; 
            b.Text = caption;
            var captionButton = (caption.Substring(0, 6) == "Region") ? "RegionButton" : "medCtrButtonStyle";
            b.ControlStyle.CssClass =captionButton;
            b.Click += new System.EventHandler(medCtrSubmit_click);


Open in new window

The values in temp are like "medButton1",  "medButton2", etc.

Question: Why btn is null at line 5 in the code below?
if (IsPostBack)
               Button btn = (Button)this.FindControl("medButton2");
               btn.BackColor = System.Drawing.Color.FromArgb(0, 138, 105, 212);
               System.Drawing.Color.FromArgb(16, btn2.BackColor);

Open in new window

Question by:Mike Eghtebas
    LVL 32

    Accepted Solution

    Cause there is no button (control) named medButton2 in this. Try this.myPanel2.FindControl().
    LVL 33

    Author Comment

    by:Mike Eghtebas
    LVL 62

    Assisted Solution

    by:Fernando Soto
    Hi eghtebas;

    I think what @steSan was trying to say is that it should be this.

    Button btn = (Button)myPanel2.FindControl("medButton2");

    Because of the following reason, from Microsoft documentation Control.FindControl Method (String):

    This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.
    LVL 1

    Assisted Solution

    n general, dynamically added controls will need to be re-added on each server round trip. The exception are control content of controls that maintain ViewState. Insure that your controls are added during PageInit which occurs before ViewState is loaded.

    Then you can simply reference your controls to acquire their contents, values, selected indexes etc.
    LVL 43

    Assisted Solution

    Also check you do actually call the makeButtonFor function for the medButton2, your question implies the code does work for medButtton1 (which does reside on the sub panel)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now