views:

91

answers:

2

I have a sudoku grid with 81 cells(asp:textboxes) which I've labeled: _c11.._c12.._c13.._c99 I'm trying to format the textboxes on page load. The current code I have is returning 81 controls, but when I do formatting in the last loop its give me a null reference error.. Excuse my horrible coding.. please :)

ArrayList sudokuCells = new ArrayList();
        for(int i = 11; i < 100; i++)
        {
            if (i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i == 70 || i == 80 || i == 90)
                continue;
            else
            {
                TextBox cell = (TextBox)Page.FindControl("_c" + i.ToString());
                sudokuCells.Add(cell);
            }
        }

        _c11.Text = sudokuCells.Count.ToString();


        foreach (TextBox cell in sudokuCells)
        {
            cell.ForeColor = System.Drawing.Color.Red;
            cell.MaxLength = 1;
        }

this is in page_load, I'm not exactly why it's giving me a null reference, because I'm referencing all textbox controls in cell then adding that reference to the arraylist.

Obviously, I'm missing something will someone please educate me?

Ok.. Page.FindControl("_c11") returns a null; even though I have them defined in my aspx page.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"            CodeBehind="Content.aspx.cs" Inherits="Sudoku.Content" %>
<asp:Content ID="_content1" ContentPlaceHolderID="head" runat="server">
<link rel="stylesheet" type="text/css" href="styles/style.css" />
 </asp:Content>
  <asp:Content ID="_content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <div id="wrapper">
   <table cellpadding="0" cellspacing="0" class="sudokuTable">
 <tr>

  <td class="column1NoBottomBorder"><asp:TextBox ID="_c11" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column2NoBottomBorder"><asp:TextBox ID="_c12" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column3NoBottomBorder"><asp:TextBox ID="_c13" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column4NoBottomBorder"><asp:TextBox ID="_c14" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column5NoBottomBorder"><asp:TextBox ID="_c15" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column6NoBottomBorder"><asp:TextBox ID="_c16" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column7NoBottomBorder"><asp:TextBox ID="_c17" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column8NoBottomBorder"><asp:TextBox ID="_c18" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
  <td class="column9NoBottomBorder"><asp:TextBox ID="_c19" runat="server" CssClass="sudokuCell"></asp:TextBox></td>
 </tr>
 <tr>
+1  A: 

FindControl is returning null somewhere in your first loop. Also:

if (i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i == 70 || i == 80 || i == 90)

How about

if (i % 10 == 0)
  continue;
aquinas
still the same results.
Jreeter
I wasn't expecting that my if statement would help, just thought it would make your code cleaner :)Try this:TextBox cell = (TextBox)Page.FindControl("_c" + i.ToString());if (cell == null) { throw new Exception("Didn't find number: " + i);}
aquinas
Thanks for your help aquinas!
Jreeter
+1  A: 

The FindControl method only finds direct children of the container that it's called on.

In your case, "Page" probably only contains one control: the form-control, that's why the textbox isn't found and you get a null reference.

Look at your code-in-front (aspx page) to find the asp server control that contains the textboxes. Is it maybe a panel? Or are the textboxes directly in the form control?

If the textboxes are in a Panel with the id "pnlBoxes", for example, you would call "FindControl" on that panel:

pnlBoxes.FindControl("_c" + i.ToString())

If that doesn't help, maybe you can post the code of your aspx page and I'm sure I can help you.

Stefan
I've added my aspx page stefan.
Jreeter
Stefan, I surrounded my table with a Panel and called .FindControl from the panel control and all works well now.. Thanks!
Jreeter