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;
for (int i = 0; i <= 30; i++)
{
if (i > 0)
{
if (newRow(sessVars[i].Nompos) == true)
{
rw = rw + 1;
}
}
if (i >= 1)
{
if (sessVars[i - 1].Keyvis == false)
{
rw = 0;
cl = cl + 2;
}
}
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);
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.