473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

passing variables to a user control

Hi

I'm new to C#, moving from ASP, and slightly confused so bear with me!

Basically I've got 4 pages, each of which runs the same user control (some
header information). I want to be able to change the data in the user control
based on the page (everything else would be duplicated, hence the user
control).

Anyway, i thought the easiest way would be to use a variable set in the
Page_load event of the calling page. So I've got a variable defined in my
page class as..

public int pageID = 1;

However, when I try to access that from the user control, it says the
variable is not defined in the namespace. What am i missing!?!

Alternatively, I could use a URL object to get the page were on directly in
the usercontrol, but I couldnt find a URL method/property to give me just the
file name.

Hope all that makes sense!

Cheers
Dan
Nov 18 '05 #1
3 1902
You should try to put a public property on your user control. In your html
code set your property.

<uc1:UserControl id="MyControl" runat="server"
MyPublicProp="1"></uc1:UserControl>

If you want to do this from code, first, you should declare your user
control in your page because visual studion didn't do this by default.

it should look like this
protected MyPath.To.Control.ControlName MyControl;
after this you can acces your control properties normally..

MyControl.MyPublicProp ="1";

Cheers
"Dan Nash" wrote:
Hi

I'm new to C#, moving from ASP, and slightly confused so bear with me!

Basically I've got 4 pages, each of which runs the same user control (some
header information). I want to be able to change the data in the user control
based on the page (everything else would be duplicated, hence the user
control).

Anyway, i thought the easiest way would be to use a variable set in the
Page_load event of the calling page. So I've got a variable defined in my
page class as..

public int pageID = 1;

However, when I try to access that from the user control, it says the
variable is not defined in the namespace. What am i missing!?!

Alternatively, I could use a URL object to get the page were on directly in
the usercontrol, but I couldnt find a URL method/property to give me just the
file name.

Hope all that makes sense!

Cheers
Dan

Nov 18 '05 #2
Hi,

Create an interface. eg:

public interface ICommonPage
{
int PageID{get;}
}

then implement it in the classes that contain the usercontrol in question
like this:

public class page1 : System.Web.UI.Page, ICommonPage
{
//Hardcode the value in a field:
int _PageID = 1; // or 2 in page2, 3 in page3, etc.

// The actual implementation:
public ICommonPage.PageID
{
get{return _PageID;}
}

// the rest of the class as it is...
}

And finally in the usercontrol cast the Page property to the interface and
get the value:

int PageID = ((ICommonPage)Page).PageID;

// work with PageID...
Hope this helps
Martin
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:1D**********************************@microsof t.com...
Hi

I'm new to C#, moving from ASP, and slightly confused so bear with me!

Basically I've got 4 pages, each of which runs the same user control (some
header information). I want to be able to change the data in the user control based on the page (everything else would be duplicated, hence the user
control).

Anyway, i thought the easiest way would be to use a variable set in the
Page_load event of the calling page. So I've got a variable defined in my
page class as..

public int pageID = 1;

However, when I try to access that from the user control, it says the
variable is not defined in the namespace. What am i missing!?!

Alternatively, I could use a URL object to get the page were on directly in the usercontrol, but I couldnt find a URL method/property to give me just the file name.

Hope all that makes sense!

Cheers
Dan

Nov 18 '05 #3
Thanks guys!

Psychos post solved it, I guess you would say, the easy way! However,
Martin's solution looks interesting.. might try that one when I know a bit
more about C# (only got it Friday!).

Would there be any advantage with ICommonPage over the second method that
Psycho suggested?

Cheers guys
Dan

"Dan Nash" wrote:
Hi

I'm new to C#, moving from ASP, and slightly confused so bear with me!

Basically I've got 4 pages, each of which runs the same user control (some
header information). I want to be able to change the data in the user control
based on the page (everything else would be duplicated, hence the user
control).

Anyway, i thought the easiest way would be to use a variable set in the
Page_load event of the calling page. So I've got a variable defined in my
page class as..

public int pageID = 1;

However, when I try to access that from the user control, it says the
variable is not defined in the namespace. What am i missing!?!

Alternatively, I could use a URL object to get the page were on directly in
the usercontrol, but I couldnt find a URL method/property to give me just the
file name.

Hope all that makes sense!

Cheers
Dan

Nov 18 '05 #4

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

Similar topics

0
by: DC Gringo | last post by:
I have a simple index.aspx page that declares and sets a sectionID in the Page_Load. The in a user control later on in the page, I read the sectionID variable (in the control's Page_Load) to...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
0
by: Neelima Godugu | last post by:
Hi All, I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar with the security settings requirement inorder to do the above. I have...
6
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"...
3
by: Philip Poole | last post by:
Hello everyone, Please help with this as it is driving me up the wall and it is urgent i finish this page, this is the first time I have used User Controls and I think I must have completly...
0
by: ben | last post by:
I am having some difficulty in understanding how to deal with variables, session states, objects, user controls while attempting to develop a ASP.Net application. I have created several user...
4
by: Tarun Mistry | last post by:
Hi, i have a page with a user control on it, the uc is a form with a submit button. On submitting, how do I make the call get picked up by the webform the user control is in? Alteratively, is...
8
by: Garry Jones | last post by:
I allows users to enter data in a form. I process the form with a php file. I return control to another page on my website. I want to dynamical create the page that they are returned to so I can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.