how can user controls reference objects on the page

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

How do I reference a datagrid on a page from a user control on that page?

In my user control, I'd like to have something like this... MyDataGrid being
on the page, not in the user control... I could probably do a byref to pass
it into one of the subs, but I heard that was not effecient...

MyDataGrid.CurrentPageIndex += 1
 
Max said:
How do I reference a datagrid on a page from a user control on that page?

In my user control, I'd like to have something like this... MyDataGrid being
on the page, not in the user control... I could probably do a byref to pass
it into one of the subs, but I heard that was not effecient...

MyDataGrid.CurrentPageIndex += 1

The user control has a property called .Page from which you can use the
FindControl method

thisControl.Page.FindControl("myDataGridID")
 
You could make a public function on the page that returns a reference to the
datagrid.
Then from within your user control you could say:
Dim dg as datagrid = Page.MyDataGridFunction()
 
Craig said:
The user control has a property called .Page from which you can use the
FindControl method

thisControl.Page.FindControl("myDataGridID")

note this should be
Page.FindControl("myDataGridID")

from inside the user control.....
 
Wow, thanks guys! Apparently I still have the old ASP mindset. Now I fully
understand WHY every page in codebehind is a public class:

Public Class myPage
Inherits System.Web.UI.Page
 
I get an error when I make the declaration::
Object reference not set to an instance of an object.

My code:
Dim dg As DataGrid = Page.FindControl("dgData")
If dg.CurrentPageIndex <> 0 Then
....
 
Your idea is giving me an error. I'm at a loss.

Error:
Object reference not set to an instance of an object.

My code inside the user control:
Dim dg As DataGrid = Page.FindControl("dgData") 'ERRORs this line
If dg.CurrentPageIndex <> 0 Then
 
None of these variations work. It seems like it's not finding the datagrid
control on the page. I can find workarounds, but I'd really like to have a
direct reference to the datagrid instance. Any ideas?

Error: Object reference not set to an instance of an object.

ASCX:
Dim dg1 As DataGrid
dg1 = CType(Page.FindControl("dgData"), DataGrid)

Dim dg1 As New DataGrid
dg1 = CType(Page.FindControl("dgData"), DataGrid)

Dim dg1 As DataGrid
dg1 = CType(Parent.FindControl("dgData"), DataGrid)


ASPX:
<asp:datagrid id="dgData" runat="server" OnSortCommand="ReSort_OnClick"
AllowSorting="true">

Codebehind:
Protected WithEvents dgData As System.Web.UI.WebControls.DataGrid
 
Sorry for all the posts, but it appears I've solved my problem.

This does work:
Dim dg1 As DataGrid
dg1 = CType(Parent.FindControl("dgData"), DataGrid)

The reason it was giving me the error was I declared the user control as a
NEW object before calling the method where this code lies, so it was unable
to find any reference to the datagrid control.

Using another set of FindControls on the parent page allowed me to refer to
the instance of the user control where this code lies, where it then finds
the datagrid control!

Well, I hope this helps someone else out there! ;)

-Max
 
Back
Top