Search code examples
c#asp.netfindcontrol

C# ASP.NET FindControl keeps returning null


I have a TextBox and a Button in my webpage. The user enters a number in the TextBox and clicks the Button, which creates (n+1) TextBoxes in a Panel.

As you can see in the code, I have assigned IDs to the TextBox instances , again when trying to access the TextBox instances using FindControl() , I keep getting a NullReferenceException for ONLY the last TextBox (ID : f1) , what am I doing wrong here ?

Initially Button1.Text is not "Find".

Code

protected void Button1_Click(object sender, EventArgs e)
{
    if (Button1.Text.Equals("Find"))
    {
        for (int i = 0; i < size; i++)
        {
            TextBox tb = (TextBox)Panel1.FindControl("Number" + i);
            n[i] = Convert.ToInt32(tb.Text);
        }
        localhost.Search s = new localhost.Search();
        resultLabel = new Label();
        TextBox tb1 = (TextBox)Panel1.FindControl("f1");
        int fNumber = Convert.ToInt32(tb1.Text);            // tb1 is null
        if (s.LinearSearch(n, fNumber))
            resultLabel.Text = "FOUND!";
        else
            resultLabel.Text = "NOT FOUND!";
        form1.Controls.Add(resultLabel);
    }
    else
    {
        size = Convert.ToInt32(TextBox1.Text);
        n = new int[size];
        TextBox1.Enabled = false;
        boxes = new TextBox[size];
        for (int i = 0; i < size; i++)
        {
            Label l = new Label();
            l.Text = "Number " + (i + 1) + " : ";
            boxes[i] = new TextBox();
            boxes[i].ID = "Number" + i;
            Panel1.Controls.Add(l);
            Panel1.Controls.Add(boxes[i]);
        }
        Label l1 = new Label();
        l1.Text = "Find Number : ";
        Panel1.Controls.Add(l1);
        findBox = new TextBox();
        findBox.ID = "f1";
        Debug.Write("[!D] ID : "+findBox.ID);
        Panel1.Controls.Add(findBox);
        Button1.Text = "Find";
    }
}

ASPX code for the page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        How Many Numbers&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="OK" />
        <br />
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>

    </div>
    </form>
</body>
</html>

Solution

  • Whenver you are creating dynamic controls you need to recreate it on postback. Hope this helps

    protected void Button1_Click(object sender, EventArgs e)
        {
            CreateControls();
            if (Button1.Text.Equals("Find"))
            {
    
                for (int i = 0; i < size; i++)
                {
                     TextBox tb = (TextBox)Panel1.FindControl("Number" + i);
                     n[i] = Convert.ToInt32(tb.Text);
                }
               localhost.Search s = new localhost.Search();
               resultLabel = new Label();
               TextBox tb1 = (TextBox)Panel1.FindControl("f1");
               int fNumber = Convert.ToInt32(tb1.Text);            // tb1 is null
             if (s.LinearSearch(n, fNumber))
               resultLabel.Text = "FOUND!";
            else
              resultLabel.Text = "NOT FOUND!";
            form1.Controls.Add(resultLabel);
           }
            else
            {
                Button1.Text = "Find";
            }
    
    
        }
    
        protected void CreateControls()
        {
            var size = Convert.ToInt32(TextBox1.Text);
            var n = new int[size];
                TextBox1.Enabled = false;
                var boxes = new TextBox[size];
                for (int i = 0; i < size; i++)
                {
                    Label l = new Label();
                    l.Text = "Number " + (i + 1) + " : ";
                    boxes[i] = new TextBox();
                    boxes[i].ID = "Number" + i;
                    Panel1.Controls.Add(l);
                    Panel1.Controls.Add(boxes[i]);
                }
                Label l1 = new Label();
                l1.Text = "Find Number : ";
                Panel1.Controls.Add(l1);
                var findBox = new TextBox();
                findBox.ID = "f1";
                Debug.Write("[!D] ID : " + findBox.ID);
                Panel1.Controls.Add(findBox);
    
            }