asp.net access label from another class - asp.net

I have a label and a button and when I press the button the label changes.
Now in order to continue i need to be able to access the label from another class. I tried to change the definition to public static in the aspx.designer.cs file but when I do that the button that changed the label before stopped working - i get a null exception. I tried to init the label but i can't find the right place to do it. Making a static method in the class, that will change the label's text didn't work as well because i can't change a non static variable in a static method.
How can I access a label from another class without getting a null exception or effecting the button that worked?

From a UserControl or a WebControl, you can access the Page with this.Page:
//From the Control File:
this.Page.TheLabel.Text = "blah";
Just make sure the label is public or you have a getter, and that you're not in a HttpContext (Not in a static method).
FIY, making the label static in the page would means there would only be one instance on the label shared by all the instance of pages. I doubt thats what you wants.

Better you can add a public property to the web control. Assign the label value to the public property and use it in any other place. It is better not to directly access label value from other files

Related

How to pass data to a ClassFactory-generated instance in Flex?

I have created an item-renderer (IListItemRenderer) to customize the display of a datagrid, and I override the data getter/setter to pass in the relevant variable. So far, so good.
In the item-renderer I have a button, which, when clicked, should display a form populated with various fields of the aforementioned variable. To do this, Button is overridden so that it displays whatever IFactory implementation it is passed.
private function instantiateDropdown():void
{
_dropdownFactory = new ClassFactory(MyPanel);
_dropdownFactory.properties = {boundData: _myVariable};
}
<buttons:PopUpButton id="editButton" dropdownFactory="{_dropdownFactory}" toolTip="Edit" source="{skins.ButtonImages.Edit}" click="edit_clickHandler(event)" />
My IFactory implementation is a panel containing my form, and it is displayed when the button is clicked, as desired. However, the variable is not set. It seems that in order to instantiate my IFactory before the overridden button needs to access it, my new ClassFactory() statement needs to be in the preinitialize event handler, but the variable has not been set at that time. Setting the properties of my IFactory instance after it has been created (e.g. in the click event) does not appear to work.
How should I pass my variable to my ClassFactory-generated instance so that it populates the form correctly?
Using lazy instantiation for the creation of the dropdownFactory (MyPanel in the question) in the class overriding Button (PopUpButton in the question), I was able to call instantiateDropdown in the creationComplete event instead of preinitialize, which allowed the PopUpButton to initialize with the correct properties (_myVariable in the question).

adobe flex static function reference control

Is it possible to reference a control in an application from a static function?
What I have is a Viewstack containing VBoxes stored in separate controls. Ex:
<mx:ViewStack id="content" width="100%" height="100%" resizeToContent="true">
<controls:Login/>
<controls:Dash/>
<controls:Input/>
<controls:Review/>
<controls:Search/>
</mx:ViewStack>
Once I get logged in on my login control, I would like to change the selected index of my ViewStack. From my outside controls, I cannot reference my ViewStack by name. I can reference a public static function from an outside control however I cannot refer to the ViewStack from within that function. Any help is greatly appreciated.
JH
Normally you can have a singleton class where you can save the
instance of the main application and if you view stack is resides
inside your main application then you can do some thing like this
public static function changeIndex(index:int):void
{
FlexGlobals.topLevelApplication.content.selectedIndex = index;
//urappinstance.content.selectedIndex = index;
}
You could reach it starting from FlexGlobals.topLevelApplication (if it is visible from there). Although, the design of such a thing may be questionable.
Is it possible to reference a control in an application from a static
function?
Generally no. A static function (or property) exists on the class itself. Whereas MXML Children--such as in a view stack--exist on a specific instance of the class. A class level function will know nothing about any specific instances of the class and will not be able to access properties on a specific instance.
However, you can pass an instance of a class into a static function and access the properties that way. something like this:
public static function doStuff(myViewStack:ViewStack):void{
trace(myViewStack.id)
// do other stuff
}
And call it like this:
MyClass.doStuff(content)

ASP.Net Custom Control - Override "Text" Property

This would seem to be very simple, but it's not working for me. I desparately need to know how to override the "Text" property of a server control I created, which inherits from "Label". When the control is dropped into an ASP Web form, I want the text property to already be set to a certain value. I tried:
[Browsable(true), Bindable(true), Category("Behavior"), Localizable(true)]
[DefaultValue("00:00:00")]
public override string Text{get; set;}
But it doesnt work; the "Text" property shows up blank - and when I try to edit it, I can change it to anything except the value specified in the "DefaultValue" attrib. This property is supposed to be overridable.
I also need to be able to set the "ID" property so it's set to a specific value when dropped on the form. Is this possible?
Any suggestions would be greatly appreciated!
For this you can use the ToolBoxDataAttribute:
Specifies the default tag generated for a custom control when it is
dragged from a toolbox in a tool such as Microsoft Visual Studio.
By default, the visual designer of a tool such as Visual Studio,
creates an empty tag. This is a tag representing a control in its
default state, when the control is dropped from the toolbox of a
visual designer onto the design surface. To specify initial default
values, a control can make use of this attribute. You can use this
attribute to customize the initial HTML content that is placed in the
designer when the control is dragged from the toolbox onto the form.
It would look something like this for your control:
[ToolBoxData("<{0}:TimeLabel ID='TL1' Text='00:00:00' runat='server' />")]
public class TimeLabel : System.Web.UI.WebControls.Label
{
[DefaultValue("00:00:00")]
public override string Text
{
get { return ViewState["Text"] != null ? (string)ViewState["Text"] : "00:00:00"; }
set { ViewState["Text"] = value; }
}
}
The DefaultValue attribute is intended to display a default value in Properties Tab.
So when you launch website without specify a value for your Text property .net uses this one.
IMHO you have not chance to do that. Sorry!

flex3:How to override function set label of a button

Flex 3 question:
I trying here to avoid having to bind resources to all my components labels ( ie a button) and find a way to have this automated.
Problem:
It corrupts the layout in design mode to bind directly in the mxml label="{resourceManager.getString('myResources', 'submit')}" and makes the design view useless. but when declaring bindings elsewhere, in actionScript or via a bind tag, it is counter productive and prone to many errors and miss.
Proposition:
I would like to create my own button that automatically invoke resources to localize a button label. So the author puts "Submit" in the mxml description of my button, and when running it would take the value of the label ie "submit" and use resourceManager.getString('myResources', 'submit').
but I can't find the way to override the set label function, Is it possible if yes how? else how can I go about it?
Maybe I am missing an essential process here that would make the use of resources more elegant, as well as how to override such thing as a button's label.
Thanks for your advices.
Create a component called MyButton, extending Button. Then use this:
override public function set label(value:String):void {
super.label = resourceManager.getString('myResources', value) || value;
}
Assuming the resource manager returns "null" or "undefined" this will work, and will only replace the value if it exists in "myResources".
If you don't want to override every component you need to do this with, then you can add a FlexEvent.CREATION_COMPLETE event on every component. Then use a single generic function to do your label localization.

Cannot access custom properties on nested user control

Ok, please understand the architecture here first.
OurMasterPage.aspx has user control Header.ascx
Header.ascx has a user control in it called LandingPageTopNav
LandingPageTopNav.ascx has a public property named "LandingPage" that is there to be able to set by the user using this control.
And I have a Third.aspx page in which I need to set the LandingPageTopNav property to a LandingPage object
The problem is that I can't get this to work in my ThirdPage.aspx.cs:
Master.LandingPageTopNav.LandingPage = this.landingPage;
Master.LandingPageTopNav.Visible = true;
And that is, I can't get the first line to work where I'm trying to reference the LandingPage property. The second line is fine. My Third.aspx definitely can reference my master page objects otherwise from code-behind.
I'd venture to guess that the LandingPageTopNav property of OurMasterPage doesn't return a value typed as LandingPageTopNav. It probably returns the correct control typed as something more generic (e.g. Control); which is why setting the Visible property works but not the LandingPage property.

Resources