473,699 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1921
You should try to put a public property on your user control. In your html
code set your property.

<uc1:UserContro l 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.Contr ol.ControlName MyControl;
after this you can acces your control properties normally..

MyControl.MyPub licProp ="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.P age, ICommonPage
{
//Hardcode the value in a field:
int _PageID = 1; // or 2 in page2, 3 in page3, etc.

// The actual implementation:
public ICommonPage.Pag eID
{
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)P age).PageID;

// work with PageID...
Hope this helps
Martin
"Dan Nash" <da*@musoswire. co.uk> wrote in message
news:1D******** *************** ***********@mic rosoft.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
1186
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 determine which panel to show. It does not work "Name 'sectionID' is not declared" is my error while compiling the user control. What am I doing wrong?
39
7647
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
6
3251
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 query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
0
2247
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 successfully gotten it working. The only problem I have is with passing parameters to the Windows Forms User control from IE I am using the Object tag to instantiate the IE object I am using param tags inside object tag to pass paramaters but the...
6
11284
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
3
1541
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 misunderstood a basic point. I have a number of user controls which are used to make up my page. One control is a series of tabs which are created dynamically from a database. When someone clicks on a tab, the page reloads and the tabs that was...
0
1036
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 controls which consist of dropdown and textbox controls on a panel. In order for one user control to access a control on another usercontrol, I have to change the WithEvents to "Protected Shared". That will allow a sub to access a control on a...
4
2562
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 it possible to post to a different .aspx page? Thanks, Taz
8
1564
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 show them a copy of what they just entered. I cant get the syntax of this. My host is set to globals on and php 4. Example Form <FORM method="post" action="process_this.php" name="anmals" id="anmals" >
0
9180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8920
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
8887
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7755
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6536
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4378
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
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
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
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.