Articles → LIGHT SWITCH 2011 → Adding Controls Dynamically in lightswitch 2011

Adding Controls Dynamically in lightswitch 2011




















Software Requirement











Prerequisite knowledge











Creation of new screen




Picture showing design mode of New data screen

Click to Enlarge


Add custom controls in the screen




Picture showing how to add custom control

Click to Enlarge



Picture showing a pop up to select StackPanel for custom control

Click to Enlarge



Custom control added

Click to Enlarge


Register ControlAvailable event in Screen’s Created event




partial void CreateNew_Created() {
	// Write your code here.
	this.FindControl("ScreenContent").ControlAvailable += new EventHandler < ControlAvailableEventArgs > (CreateNew_ControlAvailable);
}



Add code in ControlAvailable event




void CreateNew_ControlAvailable(object sender, ControlAvailableEventArgs e) {
	if (e.Control is StackPanel) {
		StackPanel spanel = (StackPanel) e.Control;

		HyperlinkButton lnk = new HyperlinkButton();
		lnk.Content = "google";
		lnk.NavigateUri = new Uri("http://google.com");
		lnk.TargetName = "_blank";
		spanel.Children.Add(lnk);

		lnk = new HyperlinkButton();
		lnk.Content = "rediff";
		lnk.NavigateUri = new Uri("http://rediff.com");
		lnk.TargetName = "_blank";
		spanel.Children.Add(lnk);
	}
}





Output


Picture of output of add controls dynamically

Click to Enlarge


Download



Posted By  -  Karan Gupta
 
Posted On  -  Thursday, August 02, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250