14 Replies - 5619 Views - Last Post: 17 February 2013 - 10:39 AM Rate Topic: -----

#1 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

row.Findcontrol not working!

Posted 15 February 2013 - 03:34 PM

Could anyone tell me why this does not work? The events seem to find it just fine - like in the RowDataBound event as the right ones show up checked. I call this sub with the RowUpdated event. All the other controls in this row are also found in both events and in this sub.

'html markup inside the TemplateField
<asp:CheckBox ID="cbWeight" runat="server" Checked='<%# Eval("WeightBased") %>' Enabled="false" />


'code behind file
Public Sub DoMath()
 For row As Integer = 0 To gvW.Rows.Count -1
  Dim cb As CheckBox = TryCast(gvW.Rows(row).FindControl("cbWeight"), CheckBox)
  If not cb Is Nothing Then 
    'it always shows as Nothing
    'also does not work
    'gvW.Rows(row).Cells(7).FindControl("cbWeight")
  End If
  'other code deleted for simplicity and cause this is the only part not working
 Next
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: row.Findcontrol not working!

#2 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: row.Findcontrol not working!

Posted 16 February 2013 - 05:03 AM

I have used this used this for finding nested controls:


    Public Shared Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
        If root.ID = id Then
            Return root
        End If

        For Each c As Control In root.Controls
            Dim t As Control = FindControlRecursive(c, id)
            If t IsNot Nothing Then
                Return t
            End If
        Next
        Return Nothing
    End Function


Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: row.Findcontrol not working!

Posted 16 February 2013 - 02:53 PM

Are you getting any errors? One thing you might try is to verify that the row is a DataRow before trying to do the FindControl. Otherwise, you may be trying to find a control in the HeaderRow and that could be causing it to error out.

In C#, it'd be something like:

if (gvW.Rows[row].RowType == DataControlRowType.DataRow)
{

} 



In VB, I think it would be something like:
If gvW.Rows(row).RowType = DataControlRowType.DataRow Then

End If



But VB.Net is not my strong point, so that might need some tweaking

This post has been edited by Nakor: 16 February 2013 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 03:06 PM

Object reference not set to an instance of an object. Which is referring to the checkbox as best as I can tell. I have added the code above and continue to get this error.

This post has been edited by _HAWK_: 16 February 2013 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: row.Findcontrol not working!

Posted 16 February 2013 - 03:27 PM

can you show the code for your OnUpdatedEvent? Calling this Sub from that event is where you are having the problem, right?

This post has been edited by Nakor: 16 February 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 03:40 PM

This is the code for a DropDownList that is the culprit in the RowUpdating event and the checkbox here if found - this is what makes no sense.

Try
      Dim gv As GridView = TryCast(sender, GridView)
      gv.EditIndex = e.RowIndex
      Dim row As GridViewRow = gv.Rows(e.RowIndex)
      If row.RowType = DataControlRowType.DataRow Then
        Dim cId As String = Session("cId").ToString
        If cId Is Nothing Then Return
        Dim lbId As Label = TryCast(row.FindControl("lbId"), Label) 'found
        Dim cbW As CheckBox = TryCast(row.FindControl("cbWeight"), CheckBox) 'found
        If Not row Is Nothing Then
          Dim itm As Item = Nothing
          Using db As New Weights_dbEntities(Util.ModelConnStr)
            Dim id As Integer = ToInt32(lbId.Text)
            Dim cid = ToInt32(cId)
            itm = db.Items.Where(Function(i) i.cId = cid And i.ItemId = id).FirstOrDefault
            Dim ddl As DropDownList = TryCast(row.FindControl("ddl"), DropDownList) 'not found
            If Not ddl Is Nothing Then itm.ItemTypeId = ToInt32(ddl.Text)
            If Not cbW Is Nothing The itm.Saved = cbW.Checked
            Try
              db.SaveChanges()
            Catch ex As Exception
              tbError.Text &= ex.Message & " " & ex.StackTrace
            End Try
          End Using
        End If
      End If
Catch ex As Exception
  tbError.Text &= ex.Message
End Try


The CheckBox above is just a problem in a math routine in first post. they both share a common problem row.FindControl cannot find them. Thanks for your help...

This post has been edited by _HAWK_: 16 February 2013 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: row.Findcontrol not working!

Posted 16 February 2013 - 03:48 PM

Ok, one more question, what does your GridView look like?
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 04:40 PM

Spoiler

Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 08:16 PM

Here is the disheartening part, I have another GridView with 2 DropDownLists and do math with it as well - and it runs perfectly! So I have a functioning example and believe you me I have combed over them for any difference and I cannot see any. I use the same 4 events to bind, edit and update the Grids. I have Rebuilt the solution, cleaned it. That it why I brought it here - in case an expert in this area could see something I am missing. There is still hope...
Was This Post Helpful? 0
  • +
  • -

#10 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: row.Findcontrol not working!

Posted 16 February 2013 - 08:41 PM

maybe try copying you're code into notepad or something, delete those files and recreate them, or maybe just trying removing the GridView, build the project, and add the GridView back in. Could be some bug in the designer file or something. Looking at your gridview and code there's definitely not anything jumping out at me as an obvious issue.

This post has been edited by Nakor: 16 February 2013 - 08:43 PM

Was This Post Helpful? 0
  • +
  • -

#11 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 09:33 PM

I have tried deleting the GridView with no help, I will try recreating the whole page now.
Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 09:57 PM

Just tried your idea - same problem! Shees, no breaks!
Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 16 February 2013 - 10:51 PM

I got it! You see where I put the label to store the items id in the same TemplateField as the item name - so there were two labels in that ItemTemplate. I made a separate TemplateField for that label and all is well and made it invisible. Does not explain why this working in the other GridView though - same layout. I am going to go with bizarro...

Persistence wins...
Was This Post Helpful? 0
  • +
  • -

#14 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: row.Findcontrol not working!

Posted 17 February 2013 - 08:50 AM

Glad you got it working, still not sure why that would keep it from finding the dropdown or checkbox, but such is the world of webforms at times.
Was This Post Helpful? 1
  • +
  • -

#15 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,053
  • Joined: 02-July 08

Re: row.Findcontrol not working!

Posted 17 February 2013 - 10:39 AM

Thanks for your time looking at it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1