![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
Variable in a User Control?
Quick question regarding user controls. If I am loading a user control, for example:
<Controls:Menu ID="MyMenu" Selected="0" runat="Server" /> where "selected" is a member variable in the user control ascx file, is it possible to use a variable for the value of "Selected"? I have variables configured in my code-behind page, which I am trying to set the selected variable to from my aspx page. Is this possible, and how is it done? Thanks in advance for your help. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
Yes it is. Create properties in your user control file. For example C#
Public System.Boolean Selected { get{ return this.selected; } set{ this.selected = value; } } Now this property is available anywhere you use the user control. So say you place the user control on a webform and call it MyMenu. Now you can say MyMenu.Selected = true; I'm not sure if this is what you are looking for because it seems that you already have Selected as a property by the statement above. If it's not, please clarify. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2005
Posts: 32
Rep Power: 0
![]() |
Hi,
There are two ways to set the properties. We defined in the user control. Let's say we have written code for "header" property of a control. We will set the "header" property declaratively in the HTML that defines the control. Change the code in ...Control.aspx to the following <p align"center"><uc1:address id="Address1" Header="Home Address" runat="server">>/uc1:address></p> By inserting Header="Home Address", you set this property while calling the user control. This behaviour is similar to passing parameters to subroutines and functions. Second, we will set propperties programmatically from the calling page's code-behind file. When you use Visual Studio.net to create user controls, you can simply build them after you create or change them. When this is done, you can set a reference to them in the calling Web Forms code-behind file. Here is the code that sets this reference: Protected Address1 As Address Private Sub btnAddress_Click(......) User Controls properties are set Address1.street=123 Forth Zv" End Sub Note: Each user control used in a Web Forms page needs its own reference, even if the same user control is used over and over again. Each user control's ID must have a line of reference. Regards bhar Books for ERP programming using .Net technologies http://www.vkinfotek.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|