-1

I have a function which loops through all the rows of my GridView and compares some of the cell values with a value the user wants to insert into the table. The point is to make sure the value they want to insert is not a duplicate value.

This function was working fine until I recently made most of the columns into EditItemTemplate controls so that I could access them on update.

Now, depending on how I try to do it, I either get a blank string or I get a "Object reference not set to an instance of an object" error.

Here's how I was originally finding the specific cell (which now apparently does nothing):

GridView1.Rows[rowIndex].Cells[2].Text

And here's how I'm doing it now (but which gives me the reference error):

GridView1.FindControl("storeNumberTB").ToString()

I suspect the issue is that I'm referencing a TextBox control within EditItemTemplate rather than just the label(?) which is what I presumably need to look at as I loop through the rows. But I can't figure out how to access the value.

So I just need to get whatever text is at a particular cell on every row as it loops through.

Here's the entire function as it is now in case that helps:

foreach (GridViewRow row in GridView1.Rows)
        {
          if (GridView1.FindControl("storeNumberTB").ToString() == userInputValue)
          {
            duplicate++;
          }
        }

Here's the relevant part of my ASPX code for the GridView:

 <asp:TemplateField HeaderText="Store">
                <ItemTemplate >
                    <asp:Label runat="server" Text='<%# Eval("Store") %>' >
                    </asp:Label>
                </ItemTemplate>
                <EditItemTemplate >
                    <asp:TextBox ID="storeNumberTB" runat="server" Text='
                    <%# Eval("Store")%>'  ></asp:TextBox>
                </EditItemTemplate>
           </asp:TemplateField>

UPDATE: row.Cells[3].Text returns an empty string. No clue why. Any idea how to access the value in the cell in my case?

Sherlock
  • 61
  • 2
  • 8

3 Answers3

2

You need to use the FindControl in the GridViewRow, not in the full control.

row.FindControl("abc")..

Also you will need to cast it to the proper control.

-Update. Misunderstood the OP question.

foreach (GridViewRow row in GridView1.Rows)
{

    var label = row.FindControl("labelname") as Label;
    ....your code...
}

Important: Also name your label. The asp:Label has no id.

0

In the foreach loop, you should use the row iterator. And I'm guessing you need to find the control and cast it as a textbox before you can get it's value. Something like this

foreach (GridViewRow row in GridView1.Rows)
{
    TextBox txtStoreNumber = row.Cells[2].FindControl("storeNumberTB") as TextBox;
    if (txtStoreNumber.Text == userInputValue)
    {
        duplicate++;
    }
}
  • But I don't think what I'm looking for is actually a Text Box? I'm afraid my explanation was unintentionally confusing. I'm trying to just loop through the GridView. The reason I even mentioned the TextBox controls is because my duplicate check stopped working once I added the TextBox controls, which makes no sense to me. It's like it wants me to access the TextBox controls, but when I try to do that, it gives me an object reference error (presumably because the loop is not looking at the TextBoxes because nothing is in edit mode), but if I try to loop through normally, nothing happens. – Sherlock Nov 27 '17 at 20:17
  • https://stackoverflow.com/questions/14584175/how-to-find-control-in-edit-item-template does this help? – Mohamed Najiullah Nov 27 '17 at 20:20
  • I don't if I'm just missing something or what. But I cannot figure out what the issue is. I tried doing row.Cells[3].Text and it returned an empty string. It seems whatever I do I either get an empty string, an object reference error, or I get a string that says something like "WebControls.CellFieldData" (can't remember exactly what it said, but something like that). – Sherlock Nov 27 '17 at 23:51
0

try with

GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
TextBox TextBox1 = row.FindControl("storeNumberTB") as TextBox; 
string storeNumberTBtext= TextBox1.Text;
user7415073
  • 290
  • 4
  • 22