.NETGURU
User Control / Codebehind
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolsvb' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Bob Levittan (VIP)
I have a user control registered like:

<%@ Register TagPrefix="bbk" TagName="navbar" src="_navbar.ascx" %>

and then used in my HTML as:

<bbk:navbar id=Navbar2 runat="server" ForeColor="Silver" Width="100%"
BackColor="LightSteelBlue"></bbk:navbar>

So far so good. Now I want to use a property of my navbar called "brand" in
my codebehind. I have this:

Protected WithEvents navbar2 As System.Web.UI.UserControl

in my codebehind page. How do I code using navbar2? I want to do something
like:

if navbar2.brand = "" then

but it says that "brand is not a member of System.Web.UI.UserControl. I know
I have to declare the control somehow to get access to it. Can someone show
me how?

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

Reply to this message...
 
    
Doug Seven

If your User Control uses code behind and is compiled in a DLL then it has
its own type (the class of the code behind).

Protected WithEvents navbar2 As MyApp.MyCustomUserControl

if navbar2.brand = "" then

If not, you need to use reflection. See:
Code Behind in Web Forms and User Controls
http://www.aspnextgen.com/tutorials.aspx?tutorialid=54

___________________________________________
Doug Seven
codejunkies.net

Are you a DotNetJunkie?
http://www.AspNextGen.com
http://www.DotNetJunkies.com

Still not enough? Buy the book:
Programming Datadriven Web Applications with ASP.NET
by Doug Seven and Don Wolthuis
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/105-60122
87-6260738

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

[Original message clipped]

Reply to this message...
 
    
Mike Amundsen
- add a code block to your user contorl
- add a public variable to the top of the code block (outside any
event-handler, class or method:
    Public brand as String

That should do it.

MCA

-----Original Message-----
From: Bob Levittan [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 09, 2001 5:31 PM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] User Control / Codebehind

I have a user control registered like:

<%@ Register TagPrefix="bbk" TagName="navbar" src="_navbar.ascx" %>

and then used in my HTML as:

<bbk:navbar id=Navbar2 runat="server" ForeColor="Silver" Width="100%"
BackColor="LightSteelBlue"></bbk:navbar>

So far so good. Now I want to use a property of my navbar called "brand" in
my codebehind. I have this:

Protected WithEvents navbar2 As System.Web.UI.UserControl

in my codebehind page. How do I code using navbar2? I want to do something
like:

if navbar2.brand = "" then

but it says that "brand is not a member of System.Web.UI.UserControl. I know
I have to declare the control somehow to get access to it. Can someone show
me how?

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
Bob Levittan (VIP)
My control is not compiled. In order to do this (through VS.NET), do I have
to create a separate project with just this control and then reference the
project from my main project?

Only 5 weeks 'til June 17th.....

========================================================

If your User Control uses code behind and is compiled in a DLL then it has
its own type (the class of the code behind).

Protected WithEvents navbar2 As MyApp.MyCustomUserControl

if navbar2.brand = "" then

If not, you need to use reflection. See:
Code Behind in Web Forms and User Controls
http://www.aspnextgen.com/tutorials.aspx?tutorialid=54

___________________________________________
Doug Seven
codejunkies.net

Are you a DotNetJunkie?
http://www.AspNextGen.com
http://www.DotNetJunkies.com

Still not enough? Buy the book:
Programming Datadriven Web Applications with ASP.NET
by Doug Seven and Don Wolthuis
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/105-60122
87-6260738

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

[Original message clipped]

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

Reply to this message...
 
    
Bob Levittan (VIP)
P.S.

If I do have to create a separate project, what type of project should it
be?

===========================================

If your User Control uses code behind and is compiled in a DLL then it has
its own type (the class of the code behind).

Protected WithEvents navbar2 As MyApp.MyCustomUserControl

if navbar2.brand = "" then

If not, you need to use reflection. See:
Code Behind in Web Forms and User Controls
http://www.aspnextgen.com/tutorials.aspx?tutorialid=54

___________________________________________
Doug Seven
codejunkies.net

Are you a DotNetJunkie?
http://www.AspNextGen.com
http://www.DotNetJunkies.com

Still not enough? Buy the book:
Programming Datadriven Web Applications with ASP.NET
by Doug Seven and Don Wolthuis
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/105-60122
87-6260738

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

[Original message clipped]

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

Reply to this message...
 
    
Paul D. Murphy (VIP)
Yes and it's a Web Custom Control Project.=20

Paul

-----Original Message-----
From: Bob Levittan [mailto:Click here to reveal e-mail address]
Sent: Thursday, May 10, 2001 11:13 AM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] RE: User Control / Codebehind

P.S.

If I do have to create a separate project, what type of project should
it
be?

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

If your User Control uses code behind and is compiled in a DLL then it
has
its own type (the class of the code behind).

Protected WithEvents navbar2 As MyApp.MyCustomUserControl

if navbar2.brand =3D "" then

If not, you need to use reflection. See:
Code Behind in Web Forms and User Controls
http://www.aspnextgen.com/tutorials.aspx?tutorialid=3D54

___________________________________________
Doug Seven
codejunkies.net

Are you a DotNetJunkie?
http://www.AspNextGen.com
http://www.DotNetJunkies.com

Still not enough? Buy the book:
Programming Datadriven Web Applications with ASP.NET
by Doug Seven and Don Wolthuis
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/105-6
0122
87-6260738

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

[Original message clipped]

| [aspngcontrolsvb] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

| [aspngcontrolsvb] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
dave 123aspx.com (VIP)

Hi Bob,
Sorry I'm a little late here, you may have already figured this out=85

I'm assuming you want to access this UserControl from codebehind of the pag=
e, not codebehind of another Usercontrol

The way I do this is to place a panel control on the page, and then load th=
e usercontrol dynamically from codebehind and add it to the panel control. =
I've listed code below, but feel free to look at other examples at:
Tutorials >> UserControls
http://www.123aspx.com/directory.asp?dir=3D10

Here is an example i use:
Option Strict On
Option Explicit On

Imports System
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class cbDefault : Inherits Page
Public pnlHeading as Panel

Public Sub Page_Load(Src As Object, E As EventArgs)
REM -- Load the control
Dim myUC as UserControl =3D LoadControl("ucHeading.ascx")

REM -- Set the Usercontrol Type
Dim ucType as Type =3D myUC.GetType()

REM -- Get access to the property
Dim ucPageHeadingProperty as PropertyInfo =3D ucType.GetProperty("PageHeadi=
ng")

REM -- Set the property
ucPageHeadingProperty.SetValue(myUC,"Access a Usercontrol from Code Behind"=
,Nothing)

pnlHeading.Controls.Add ( myUC )
End Sub

End Class
.

Cheers!
dave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.123aspx.com
The Largest ASP.NET Web Directory!
Find the latest ASP.NET resources --
Subscribe to our newsletter!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------- Original Message ----------------------------------
From: "Doug Seven" <Click here to reveal e-mail address>
Reply-To: "aspngcontrolsvb" <Click here to reveal e-mail address>
Date: Wed, 9 May 2001 19:07:44 -0700

[Original message clipped]

Reply to this message...
 
    
Robert K Breton \(BOB\)
The only reason I wanted to put C# code with MY VB Code was because this
IBuySpy Module PageCounter is in C#, and I got the VB Version of the Portal
site. They are asking for a declaration in C# to be placed in my Global.asax
file, that is using VB as the language.

I bet its easier to just recode the C# declaration into VB Code than trying
to Figure out how to Link 2 CodeBehinds to 1 Global.asax using 2 seperate
languages.

void Application_Start(Object sender, EventArgs e)

{

Application["HitCounter"] = (long) 0;

Application["HitCounterPortalID"] = "";

Application["HitCounterLoaded"] = "";

Application["HitCounterDirtyCache"] = "false";

}

void Session_Start(Object sender, EventArgs e)

{

string strSessionID = Session.SessionID;

if (Application[strSessionID] == null)

{

// log a new session has started

Application[strSessionID] = strSessionID;

long l = (long) Application["HitCounter"];

l += 1;

Application["HitCounter"] = l;

// mark cache as dirty

Application["HitCounterDirtyCache"] = "true";

}

}

Theres the Code they want inside my Global.asax

I understand the VB.NET, But C#.NET Is still relatively new, and these
commands are all new to me.

Bob

Can someone convert this to VB for me, so I can see how?

----- Original Message -----
From: "dave 123aspx.com" <Click here to reveal e-mail address>
To: "aspngcontrolsvb" <Click here to reveal e-mail address>
Sent: Wednesday, May 23, 2001 9:18 AM
Subject: [aspngcontrolsvb] RE: User Control / Codebehind

Hi Bob,
Sorry I'm a little late here, you may have already figured this out.

I'm assuming you want to access this UserControl from codebehind of the
page, not codebehind of another Usercontrol

The way I do this is to place a panel control on the page, and then load the
usercontrol dynamically from codebehind and add it to the panel control.
I've listed code below, but feel free to look at other examples at:
Tutorials >> UserControls
http://www.123aspx.com/directory.asp?dir=10

Here is an example i use:
Option Strict On
Option Explicit On

Imports System
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class cbDefault : Inherits Page
Public pnlHeading as Panel

Public Sub Page_Load(Src As Object, E As EventArgs)
REM -- Load the control
Dim myUC as UserControl = LoadControl("ucHeading.ascx")

REM -- Set the Usercontrol Type
Dim ucType as Type = myUC.GetType()

REM -- Get access to the property
Dim ucPageHeadingProperty as PropertyInfo =
ucType.GetProperty("PageHeading")

REM -- Set the property
ucPageHeadingProperty.SetValue(myUC,"Access a Usercontrol from Code
Behind",Nothing)

pnlHeading.Controls.Add ( myUC )
End Sub

End Class
.

Cheers!
dave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.123aspx.com
The Largest ASP.NET Web Directory!
Find the latest ASP.NET resources --
Subscribe to our newsletter!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------- Original Message ----------------------------------
From: "Doug Seven" <Click here to reveal e-mail address>
Reply-To: "aspngcontrolsvb" <Click here to reveal e-mail address>
Date: Wed, 9 May 2001 19:07:44 -0700

[Original message clipped]

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
 
System.EventArgs
System.Reflection.PropertyInfo
System.Web.UI.UserControl
System.Windows.Forms.UserControl




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification