Click here to Skip to main content
14,391,415 members

Cannot use findcontrol to retrieve textbox in gridview - dual datasources

Ray Fischer asked:

Open original thread
I am trying to assign a value to a read-only TextBox which is lodged inside a DataGrid in a webpage. The TextBox is being reconstructed with each Postbox using automation called Add_TextBoxes. The TextBoxes inside the DataGrid can either be modified themselves by making an entry within the box (calls a method, TextBox_Changed) which is assigned idndividually, dynamically to each TextBox when they are created, or else via the automation which is causing me the problem. The automated call to update the textboxes within the grid is anchored inside longer textboxes, which are parsed based on a certain logic to distribute pieces of the Text (Substrings) to each of the textboxes in the grid. The problem I am having is that the textboxes inside the Grid are not being found. I am getting a Null Error exception. Here are some snippets of Code -

Inside my Page_Load (nested IF to call adding textboxes to Grid after Postback)

   else if (Session["SrcCtrl"].ToString() == "Noms")
    {
        this.Add_TxtBoxes();
        this.parse_Nomkey();
    }
}


Here is the Add_TxtBoxes method:

protected void Add_TxtBoxes()

        {

            List<sessVar> sessVars = ret_sv_data();
            int rw = 0;
            int cl = 0;

            // Cursor through all 31 positions

            for (int i = 0; i <= 30; i++)
            {

                //  Check to see if a new row for the data table / data grid is needed, - skipped on first iteration (row = 0)

                if (i > 0)
                {
                    if (newRow(sessVars[i].Nompos) == true)
                    {
                        rw = rw + 1;
                    }
                }

                //  Check to see if a column change is needed (col + 2), as we move through each component of the nomenclature

                if (i >= 1)
                {
                    if (sessVars[i - 1].Keyvis == false)
                    {
                        rw = 0;
                        cl = cl + 2;
                    }
                }

                //  If session variable is visible in the grid, add its text box and description one column over

                if (sessVars[i].Keyvis == true)
                {
                    Control thsctrl = this.FindControl("txtBoxNomPt" + rw + cl);

                    TextBox txtBxNm = new TextBox();
                    txtBxNm.Width = 48;
                    txtBxNm.ID = "txtBoxNomPt" + rw + cl;
                    txtBxNm.AutoPostBack = true;
                    txtBxNm.Attributes.Add("runat", "server");
                    txtBxNm.TextChanged += new EventHandler(TextBox_TextChanged);
                    this.NomFlds.Rows[rw].Cells[cl].Controls.Add(txtBxNm);
                    {
                        if (!string.IsNullOrEmpty(sessVars[i].Value as string) || sessVars[i].Svexists == true)
                        {
                            txtBxNm.Text = sessVars[i].Value.ToString();
                            if (sessVars[i].Value.Substring(0, 1).ToString() == "*")
                            {
                                this.NomFlds.Rows[rw].Cells[cl + 1].Text = sessVars[i].PosDesc.ToString();
                            }
                            else
                            {
                                TextBox_TextChanged(txtBxNm, EventArgs.Empty);
                                txtBxNm.Text = Session[sessVars[i].Sessnm].ToString();
                            }
                        }
                    }
                }
            }
        }



Here is the method for assigning a TextBox within the Grid a value after a user has updated one of the longer String 'macro' textboxes and tabbed out to start the parsing sequence -

public void parse_str_arr(int[] pos, string[] cts, string inp_str)
  {
      int startpt = 0;
      String thsval = "";
      for (int i = 0; i < pos.Length; i++)
      {
          if (pos[i] != -1)
          {
              string prow = cts[i].Substring(0, 1).ToString();
              int ctrw = Int32.Parse(prow);
              GridViewRow thsrow = this.NomFlds.Rows[ctrw];
              String ctrlnm = "txtBoxNomPt" + cts[i].ToString();
              Control thsfld = this.FindControl(ctrlnm);
             // Control [] thsfldarr = Retr_Set(ctrlnm, ctrlnm.Length);
             // Control thsfld = thsfldarr[1];
              TextBox thsfldtxt = thsfld as TextBox;
              if (inp_str.Length >= (startpt + PoslengthA[i]))
              {
                  thsval = inp_str.Substring(startpt, PoslengthA[i]).ToString();
                  thsfldtxt.Text = thsval;
                  TextBox_TextChanged(thsfld, EventArgs.Empty);
                  Session[SessnameA[pos[i]]] = thsval;
              }
              else
              {
                  thsval = ValueA[pos[i]];
                  thsfldtxt.Text = thsval;
                  TextBox_TextChanged(thsfld, EventArgs.Empty);
                  Session[SessnameA[pos[i]]] = thsval;
              }
              startpt = startpt + PoslengthA[pos[i]];

          }
      }
  }



Here is where my error is occurring. The Control thsfldtxt is not being found. In Rendering I can see the textboxes and I know they are there. But I am not finding them. Either there are hidden postbacks which are blowing away the names or else I have a syntax issue somewhere. I wrote a function to reassign the names but I am not sure where to build it in. This one has really got me on the run!

I use a great number of arrays to control this application because it is a lot of string manipulation using fixed rules. So if there are questions about the arrays and their function, feel free to ask.

What I have tried:

Tried to use a cool tool I found on the NET for getting at controls inside DataGrids called FlattenHierarchy which is supposed to remove the data container issue for finding a Control which is nested but that didn't work either.
Tags: C#, ASP.NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100