0

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>
cyberpirate92
  • 3,076
  • 4
  • 28
  • 46
  • Did you re-create the dynamically generated textboxes on post back? – User2012384 Jan 15 '15 at 07:40
  • @Mr.香港人 : I'm sorry, I don't know what that means as I am new to c# and ASP.NET. – cyberpirate92 Jan 15 '15 at 07:43
  • I just modified your code a bit for debugging, but was unable to reproduce your issue, instead, I even can't get the first textbox, here's the link: https://dotnetfiddle.net/11RDQx Did I miss something?? – User2012384 Jan 15 '15 at 07:46
  • @Mr.香港人 please refer this: https://dotnetfiddle.net/r339uA – cyberpirate92 Jan 15 '15 at 07:50
  • Still unable to reproduce.. are you sure you can receive ALL textboxes except the last one on Button1_Click event..? you have to re-create the textboxes on Post back or else all textbox will lose.. – User2012384 Jan 15 '15 at 07:54
  • @Mr.香港人: Yes, I am sure. The `TextBox` with ID `f1` is the only one that fails. – cyberpirate92 Jan 15 '15 at 07:56
  • Try adding Panel1.FindControl("f1").GetType(); what's the type of f1?? – User2012384 Jan 15 '15 at 07:57
  • Something wrong with the code. You are looping in codebehind that means you are expecting multiple controls while aspx mark up shows only one text box. Please explain scenario of what you want to achieve. – Amit Jan 15 '15 at 07:59
  • @Mr.香港人 : `Debug.Write("[D!] "+Panel1.FindControl("f1").GetType());` , unable to execute as I am getting a `NullReferenceException`. – cyberpirate92 Jan 15 '15 at 08:00
  • That means Control "f1" wasn't in Panel1... Try printing out the control names in Panel1.. – User2012384 Jan 15 '15 at 08:02
  • @Amit : Initially I have a textbox and a button, I am trying to get a number 'n' as input and upon click of the button, add 'n+1' textboxes dynamically to the panel. All the textboxes are getting added and when I am trying to get their values, I am able to retrieve the values of all the textboxes except the last one with ID f1, where I am getting a `NullReferenceException`. – cyberpirate92 Jan 15 '15 at 08:04

2 Answers2

0

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.

MANOJ GOPI
  • 1,279
  • 10
  • 31
0

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

        }
Amit
  • 882
  • 6
  • 13