0

I am creating 5 radio button when my page is loading :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            for (int i = 0; i < 5; i++)
            {
                RadioButton r = new RadioButton();
                r.Text = i.ToString();
                r.ID = i.ToString(); ;
                Panel1.Controls.Add(r);

            }
        }
    }

I would like to access them in another method correspond to a click button, but I cannot. :

protected void Button1_Click(object sender, EventArgs e)
    {            
        RadioButton r = (RadioButton)FindControl("2");
        r.Checked = true;             
    }

When I am doing my findcontrol method, I get the following exception : nullreferenceexception was unhandled by user code

4
  • But I don't understand how I can access to my radio buttons created in page_load method. Commented Oct 31, 2014 at 12:54
  • @SonerGönül The question has a specific problem. "How to access dynamic controls". The answer should contain the details related to this problem. The question you marked as duplicate doesn't completely answer the question. Commented Oct 31, 2014 at 12:58
  • Ok, I reopened your question since it is not entirely related to NullReferenceException. Commented Oct 31, 2014 at 12:59
  • If you ONLY create the controls on non-postback, they won't be available during a postback. Commented Oct 31, 2014 at 13:11

3 Answers 3

2

You have added the controls in the Panel1, so you should find it in there.

Replace the line:

RadioButton r = (RadioButton)FindControl("2");

with:

RadioButton r = Panel1.FindControl("2") as RadioButton;
if(r != null)  //check for null reference, before accessing
    r.Checked = true;
Sign up to request clarification or add additional context in comments.

2 Comments

ok, and I also have to remove the if postback condition in my page_load method isn't it?
Yes you need to remove it if you don't want to lose your controls on post back.
1

FindControl does not do deep search. You added radio buttons to Panel1, but calling FindControl of Page.

RadioButton r = (RadioButton)Panel1.FindControl("2");

Another thing. Remove if (!Page.IsPostBack) condition. When Button1_Click fires, the page is in PostBack state and dynamic controls have to be created if you expect to find them.

1 Comment

There is another function that goes deeper recursively but you wont need that. I think that Igor has the solution.
0

You need to check that is controls are created or not and need to check null value. You are doing it a wrong way. To solve this error First check object is initialized or not if it is Initialized that means value is not received by reference variable. Please check the following link for reference: http://blog.mastersoftwaresolutions.com/why-null-reference-error-occurred/

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.