473,624 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can user controls reference objects on the page

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.Curr entPageIndex += 1
Nov 18 '05 #1
9 1909
Max wrote:
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.Curr entPageIndex += 1


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

thisControl.Pag e.FindControl(" myDataGridID")

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #2
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.MyDataGrid Function()

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Max" <ma*****@portvi sta.com> wrote in message
news:kx******** *************@t wister.tampabay .rr.com...
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.Curr entPageIndex += 1

Nov 18 '05 #3
Craig Deelsnyder wrote:
Max wrote:
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.Curr entPageIndex += 1


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

thisControl.Pag e.FindControl(" myDataGridID")


note this should be
Page.FindContro l("myDataGridID ")

from inside the user control.....

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #4
Craig Deelsnyder wrote:
Craig Deelsnyder wrote:
Max wrote:
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.Curr entPageIndex += 1


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

thisControl.Pag e.FindControl(" myDataGridID")


note this should be
Page.FindContro l("myDataGridID ")

from inside the user control.....

sorry, this reply was for my thread above

--
---
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #5
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.P age
"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote

note this should be
Page.FindContro l("myDataGridID ")

from inside the user control.....

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 18 '05 #6
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.FindContro l("dgData")
If dg.CurrentPageI ndex <> 0 Then
....

"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote
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.MyDataGrid Function()

Nov 18 '05 #7
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.FindContro l("dgData") 'ERRORs this line
If dg.CurrentPageI ndex <> 0 Then
"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote
Page.FindContro l("myDataGridID ")

from inside the user control.....

Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 18 '05 #8
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.Find Control("dgData "), DataGrid)

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

Dim dg1 As DataGrid
dg1 = CType(Parent.Fi ndControl("dgDa ta"), DataGrid)
ASPX:
<asp:datagrid id="dgData" runat="server" OnSortCommand=" ReSort_OnClick"
AllowSorting="t rue">

Codebehind:
Protected WithEvents dgData As System.Web.UI.W ebControls.Data Grid

"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote in

note this should be
Page.FindContro l("myDataGridID ")

from inside the user control.....

Nov 18 '05 #9
Max
Sorry for all the posts, but it appears I've solved my problem.

This does work:
Dim dg1 As DataGrid
dg1 = CType(Parent.Fi ndControl("dgDa ta"), 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

"Craig Deelsnyder" <cdeelsny@NO_SP AM_4_MEyahoo.co m> wrote in message
news:ef******** ******@tk2msftn gp13.phx.gbl...
Craig Deelsnyder wrote:
Craig Deelsnyder wrote:
Max wrote:

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.Curr entPageIndex += 1

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

thisControl.Pag e.FindControl(" myDataGridID")


note this should be
Page.FindContro l("myDataGridID ")

from inside the user control.....

sorry, this reply was for my thread above

--
---
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3013
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have access to the methods and properties from the Page_Load, no problem there. But as soon as I want access to the user control from another procedure on the same page, I get the next error message: "Object reference not set to an instance of an...
6
11276
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
4
1355
by: Andrea Williams | last post by:
I'm trying to set up a user control and change some values from the aspx page, but I keep running into trouble. What I really would like to do is be able to set a Label.Text in the user control from an aspx page. If I open up a static method to set the Label in the code-behind of the user control(uc), then the code in the uc doesn't recognize that the label exists (only if the method is a static (shared) method). If I set up a private...
10
2491
by: George G. | last post by:
Hi there, I am busy writing a new asp.net application and I am reusing some of my existing asp functions and methods in a user control. I need access to session, request and response in some of the functions and I can't find out how to do it. Here is an example of what I do and I get the following Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the...
7
3187
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb -- UC/ ----- Classic/
3
1928
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the page. 2.) The identifier must be delcared with a minimum access of 'Protected' in order to work. If the above is indeed correct, then I have made the following assumptions
9
2597
by: abprules | last post by:
Can somehow tell me the best way for multi user development to occur in MS Access? The situation is: We are creating a new database for a small company. There are 2 of us who want to simultaneously work on development. Does splitting the database and creating FE's work for this scenario? And if so, would the changes that both users make be saved into the original .mdb? Is there any good source that I can read more about this in detail...
4
7585
by: Eric | last post by:
I got a particular problem in visual studio 2005 There's a user control on page and I want to meka a cast like this MyPage mp=(MyPage)this.Page; Error is : cannot cast from ASP.mypage_blalala_aspx to MyPage. Stupid namespace ASP doesn't have that class name. I can't even put any base classes to that crazy App_Code folder to get the class reference. Am I doing something wrong? All I want to do is during click to access a mainpage...
3
1923
by: Fred Chateau | last post by:
I am trying to access a user control class, for a user control that is loaded dynamically, from the containing page. I have been able to access Web controls in the user control, but so far I have been unable to expose the user control class itself. I'm guessing that I need to set up an interface, but I am not sure how to accomplish this. Here is the code that loads the user control: protected void Page_Init(object sender, EventArgs e)...
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8619
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.