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

Adding Controls Dynamically in lightswitch 2011





  1. Software Requirement
  2. Prerequisite knowledge
  3. Creation of new screen
  4. Add custom controls in the screen
  5. Register ControlAvailable event in Screen’s Created event
  6. Add code in ControlAvailable event
  7. Output

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