C# ASP.NET FindControl 一直返回 null

C# ASP.NET FindControl keeps returning null

我的网页中有一个 TextBox 和一个 Button。用户在 TextBox 中输入一个数字并单击 Button,这会在 Panel 中创建 (n+1) TextBox 个元素。

正如您在代码中看到的,我已将 IDs 分配给 TextBox 实例,再次尝试使用 FindControl() 访问 TextBox 实例时, ONLY 最后一个 TextBox (ID : f1) 我总是得到一个 NullReferenceException,我在这里做错了什么?

最初 Button1.Text 不是 "Find"

代码

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 代码

<%@ 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>

尝试在整个页面中搜索 ID f1,而不是在面板中搜索。

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

在您的文本框中添加 ID

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

在您的 aspx 页面中没有任何带有 f1 ID 的内容,因此您在尝试获取元素的文本时遇到空引用异常。

无论何时创建动态控件,都需要在回发时重新创建它。希望这有帮助

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

        }