how can user controls reference objects on the page

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
 
C

Craig Deelsnyder

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")
 
S

Steve C. Orr [MVP, MCSD]

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()
 
C

Craig Deelsnyder

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.....
 
M

Max

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
 
M

Max

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
....
 
M

Max

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
 
M

Max

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
 
M

Max

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top