简体   繁体   中英

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) TextBox es in a Panel .

As you can see in the code, I have assigned ID s 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>

Try searching the whole page for ID f1 rather than searching in a panel.

TextBox tb1 = (TextBox)FindControl("f1");

Add the ID in your textbox

<asp:TextBox ID="f1" runat="server"></asp:TextBox>

In your aspx page there is nothing with f1 ID because of which you were getting the null reference exception when trying to get the element's Text.

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);

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM