I have tried using FindControl which is what every single StackOverflow topic suggests, and I get an object reference error. Is there no other way? I found one topic where the guy seemed to be having the same problem as me but nobody answered his either.
I am trying to loop through my GridView and do a comparison, but row.Cells[3].Text returns a blank string. I assume this is because I have TextBox controls in my GridView, but when I try to access the TextBox controls OUTSIDE OF AN EDIT/UPDATE event I get an "Object reference not set to instance of object" error. It makes sense to me that I get this error because the TextBox is not "open" at the time I'm looping through the GridView.
So this is my dilemma. I can't get the value the ordinary way (row.Cells[3].Text) and I can't seem to get it by using FindControl either.
Is there some other way I'm supposed to retrieve the value in this case?
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[3].Text == storeNumber)
{
// blah blah
}
}
Alternatively,
foreach (GridViewRow row in GridView1.Rows)
{
if (row.FindControl("StoreNumberLabel").ToString() == storeNumber)
{
// blah blah
}
}
I have also tried casting the row.FindControl as a TextBox and using .Text instead of .ToString(), but same result.
I have read countless StackOverflow questions about related situations and I cannot find anything that relates to my specific question. If there is a topic that already addresses this, PLEASE link me. I have looked all over the place for a solution to this and cannot find anything.
Update: This is my aspx code
<asp:TemplateField HeaderText="Store">
<ItemTemplate>
<asp:Label ID="StoreNumberLabel" runat="server" Text='<%# Eval("Store") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="storeNumberTB" runat="server" Text=' <%# Eval("Store")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
UPDATE2
I have sort of got it working. The trick is to check if row.FindControl("yourControl") is null. NOT the .Text of the TextBox, but just the row.FindControl().
Problem now is that it doesn't seem to be looking at every row. Only certain ones (which I'm guessing are for some reason the ones that aren't null?).