Articles → LIGHT SWITCH 2011 → Adding Controls Dynamically In Lightswitch 2011

Adding Controls Dynamically In Lightswitch 2011






Software Requirement




  1. Visual studio 2010 is installed on your machine.
  2. Visual Studio 2010 service pack 1 is installed on your machine.
  3. Microsoft Visual Studio Light switch 2011 is installed on your machine

Prerequisite Knowledge




  1. How to add screen in lightswitch?
  2. What is Created event in lightswitch?
  3. What is ControlAvailable event in lightswitch?

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 2, 2012

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250