Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"FindControl" not working.. 1

Status
Not open for further replies.

PsychoCoder

Programmer
May 31, 2006
140
US
I have a page where Im trying to bind a repeater, the code for binding said repeater is as follows:

Code:
Protected Sub GetApps()
  sSQL = "SELECT "
  sSQL &= "RTRIM(column1) as 'change_id',RTRIM(column2)  as 'app_name',RTRIM(column3) as 'app_description',"
  sSQL &= "RTRIM(column4) as 'app_developer',RTRIM(CONVERT(VARCHAR(15),column5)) as 'app_completition_date' "
  sSQL & = " **Table_Name** "
  sSQL &= "ORDER BY date_entered"
  Command = New SqlCommand(sSQL, Connection)
  Command.CommandType = CommandType.Text
  DAdapter = New SqlDataAdapter
  DAdapter.SelectCommand = Command
  DS = New DataSet
  Try
    Connection.Open()
    DAdapter.Fill(DS, "**Table_Name**")
    Pager.DataSource = DS.Tables("**Table_Name**").DefaultView
    Pager.AllowPaging = True
    Pager.PageSize = 25
    Pager.CurrentPageIndex = CurrentPage
    CType(Me.current_apps.FindControl("lblCurrentPage"), Label).Text = "Page: " + (CurrentPage + 1).ToString + " of " + Pager.PageCount.ToString
    cmdPrev.Enabled = Not Pager.IsFirstPage
    cmdNext.Enabled = Not Pager.IsLastPage
    current_apps.DataSource = Pager
    current_apps.DataMember = "column1"
    current_apps.DataBind()
  Catch ex As Exception

  End Try
End Sub
The object I'm using in this sub are declared at the top of the page:

Code:
Private Shared Conn As String = Common.GetConnectionString("api_sap")
Private Shared Connection As New SqlConnection(Conn)
Private Command As SqlCommand
Private Shared DReader As SqlDataReader
Private Shared DAdapter As SqlDataAdapter
Private Pager As PagedDataSource = New PagedDataSource
Private DS As DataSet
Private Shared sSQL As String

At first I thought my sub wasnt returning any rows (as the page was blank) but that was because of the empty Catch, which wasnt allowing any errors to show. I removed that line and get this error:

Code:
Object reference not set to an instance of an object. 
[b]Description:[/b] An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

[b]Exception Details:[/b] System.NullReferenceException: Object reference not set to an instance of an object.

[b]Source Error: [/b]


Line 47: 			Pager.PageSize = 25
Line 48: 			Pager.CurrentPageIndex = CurrentPage
[b]Line 49: 			CType(Me.current_apps.FindControl("lblCurrentPage"), Label).Text = "Page: " + (CurrentPage + 1).ToString + " of " + Pager.PageCount.ToString[/b]
Line 50: 			cmdPrev.Enabled = Not Pager.IsFirstPage
Line 51: 			cmdNext.Enabled = Not Pager.IsLastPage

The HTML code for the repeater is:

Code:
<ASP:REPEATER ID="current_apps" RUNAT="server" DATAMEMBER="change_id" VISIBLE="true" >
  <HEADERTEMPLATE>
    <table id="header" width="100%" cellpadding="2" cellspacing="2" class="list_header">
      <tr>
        <td colspan="5">
	[b]<ASP:LABEL ID="lblCurrentPage" RUNAT="server" TEXT="Label"/>[/b]</td>
      </tr>					
      <tr>
        <td colspan="5">&nbsp;</td>
      </tr>
      <tr> 
         <td style="WIDTH: 100px">&nbsp;</td>
         <td><ASP:LABEL ID="Label2" RUNAT="server" TEXT="Application Name"/></td>
         <td><ASP:LABEL ID="Label3" RUNAT="server" TEXT="Description"/></td>
         <td><ASP:LABEL ID="Label4" RUNAT="server" TEXT="Developer"/></td>
          <td><ASP:LABEL ID="Label5" RUNAT="server" TEXT="Completition Date"/></td>
       </tr>
     </table>
   </HEADERTEMPLATE>
   <ITEMTEMPLATE>
     <table id="header" cellpadding="2" cellspacing="2" width="100%">
       <tr>
         <td style="WIDTH: 100px">
         <ASP:LINKBUTTON ID="lnkSelect" RUNAT="server" TEXT="Edit" COMMANDNAME="Edit"/></td>
         <td><ASP:LABEL ID="Label2" RUNAT="server" TEXT='<%# DataBinder.Eval(Container.DataItem,"app_name") %>'/></td>
         <td><ASP:LABEL ID="Label3" RUNAT="server" TEXT='<%# DataBinder.Eval(Container.DataItem,"app_description") %>'/></td>
          <td><ASP:LABEL ID="Label4" RUNAT="server" TEXT='<%# DataBinder.Eval(Container.DataItem,"app_developer") %>'/></td>
          <td><ASP:LABEL ID="Label5" RUNAT="server" TEXT='<%# DataBinder.Eval(Container.DataItem,"app_completition_date") %>'/></td>
        </tr>
      </table>
    </ITEMTEMPLATE>
</ASP:REPEATER>

I have bolded the line where lblCurrentPage does indeed exist. So why is FindControl not working? Anyone have any ideas?

By the way, CurrentPage is a property in the code behind:

Code:
Public Property CurrentPage() As Integer
  Get
    Dim o As Object = Me.ViewState("_CurrentPage")
    If o Is Nothing Then
      Return 0
    Else
      Return CType(o, Integer)
    End If
  End Get
  Set(ByVal value As Integer)
    Me.ViewState("_CurrentPage") = value
  End Set
End Property

Can someone please help me with this

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
** Do NOT feed Code Gremlins after midnight **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
I have bolded the line where lblCurrentPage does indeed exist. So why is FindControl not working? Anyone have any ideas?
Yes it exists, but it exists as part of the repeater and therefore it's parent isn't "Me" which quite rightly can't find any control named "lblCurrentPage".


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

Then what do I use? Ive tried current_apps.FindControl and then just FindControl and both have failed.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
** Do NOT feed Code Gremlins after midnight **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
this current_apps.FindControl did not work because it is inside the HeaderTemplate. (I am just guessing here)

Try to move it to Itemtemplate and see if you can access it just for testing purposes...

-DNG
 
Problem Solved!

Come to find out theres an issue with a Repeater that if your control is inside a header or footer template you cant access that control until after the repeater is bound so I mived that line of code underneath the bind line and it works great. Thanks to everyone for your help, and DotNetGnat I wouldnt have found that without the links you posted.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
** Do NOT feed Code Gremlins after midnight **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
theres an issue with a Repeater that if your control is inside a header or footer template you cant access that control until after the repeater is bound
That isn't true. If you want to access any items in the Header/Footer etc then you can do so by using the ItemCreated event and checking the type of Item that was created.

Code:
    Protected Sub Repeater1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemCreated
        If e.Item.ItemType = ListItemType.Header Then
            ...
        End If
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

I needed to be able to do it when the repeater was bound and moving it below the bind statement was the only way I could get it to work.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
** Do NOT feed Code Gremlins after midnight **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Yes I know, I was just pointing out an alternative method that you could have used.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top