AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1521&pId=-1
Working with Dynamic Populate Extender
page
by Nidal Arabi
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35715/ 62

Introduction

I always wanted to be able to fill a panel with information gathered from all around the page directly after my end user selected his options and present them into a summary. Dynamic populate extender is a nice controller of a panel that is expected to be filled dynamically at the choices of the user. This article will show an example on how I used the control to provide the user with feedback about the extra preferences that a customer would select on a page. The whole project is attached for your own review. Let us examine my simple Pizza selection screen.

Requirements

In order to be able to work with ASP.NET AJAX Control Toolkit, the following should be available:

·         Microsoft Visual Studio 2005 (any version) or you can also use the free of cost Visual Web Developer Express 2005.

·         Download ASP.NET 2.0 AJAX Extensions and install them on your computer after installing Visual studio 2005.

·         Download ASP.NET AJAX Control Toolkit and install them on your computer after installing Visual Studio 2005 and AJAX Extensions. You can also download the Toolkit directly from CodePlex project site.

·         After the completion of installation, open Visual Studio 2005 or Visual Web Developer and add a tab in the Toolbox named AJAX Toolkit.

·         Right Click in this tab and choose add items. Browse to the sample directory of where you did extract the Toolkit, then to bin directory and choose the file named AjaxControlToolkit.dll.

This is all. You have installed AJAX Control Toolkit and you can start building your controls.

Well, you should be ready by now to begin AJAX Toolkit implementation.

Creating your Dynamic populate sample

Follow these easy steps to create your solution.

1.    Start your Visual Studio 2005 IDE.

2.    Choose Create Web Site from the menu.

3.    In this sample application we will use the Visual Basic language for the sample application.

4.    Name the solution DynamicPopulateSample.

5.    Choose ASP.NET AJAX Control Toolkit Web Site.

6.    Choose File System in the location box.

7.    Click OK to create the project.

8.    Visual Studio 2005 will create your project with a Default.aspx page and likely a readme.txt. Go ahead and get rid of the latter file.

9.    Open Default.aspx page in design view.

10. You noticed that there is a control on the page already called script manager. (Well, AJAX is really a script based implementation of the language JavaScript.) In Short, Script Manager Control manages client script for Microsoft ASP.NET AJAX pages. By default, the Script Manager control registers the script for the Microsoft AJAX Library with the page. This enables client script to use the type system extensions and to support features such as partial-page rendering and Web-service calls.

11. Inside the form tag, insert listing 1.

Listing 1: HTML Listing For Creating the checkboxes

<br />
<label for="Chk1"><input id="Chk1" type="checkbox"  name="chkpizza" 
    value="Extra Cheese" onclick="UpdatePizza(3);" />Extra Cheese</label><br />
<label for="Chk2"><input id="Chk2" type="checkbox"  name="chkpizza" value="Olive"  
    onclick="UpdatePizza(3);" />Olive</label><br />
<label for="Chk3"><input id="Chk3" type="checkbox"  name="chkpizza" 
    value="Mushrooms"  onclick="UpdatePizza(3);" />Mushrooms</label><br />
<br />

12. Also, directly after the form tag insert the JavaScript introduced in listing 2.

Listing 2: JavaScript Listing

<script type = "text/javascript"> 
  function UpdatePizza(chkcount)
  {
  var behavior = $find('dp1');
  var value = '';
  for (i = 0; i < chkcount; i++)
  {
    if (document.forms[0].chkpizza[i].checked == true)
      value = ' With ' + document.forms[0].chkpizza[i].value + ',';
  }
  if (behavior)
  {
    behavior.populate(value);
  }
}
 
Sys.Application.add_load(function()
{
  UpdatePizza(3);
}
 
);
</script>

13. Drag a panel below the checkboxes with no text in it.

14. Drag a dynamic populate extender and drop it right after the panel.

15. Formulate the dynamic populate in HTML view as described in listing 3.

Listing 3: HTML formatting of Dynamic Populate Extender

<cc1:DynamicPopulateExtender ID="DynamicPopulateExtender1" runat="server" 
    TargetControlID="Panel1" BehaviorID="dp1" ServiceMethod="GetPizza">
</cc1:DynamicPopulateExtender>

16. Last but not least, copy and insert listing 4 after the title section in the header part.

Listing 4: Web Service listing to update the panel

<script runat="server">
  <System.Web.Services.WebMethod()> _
  <System.Web.Script.Services.ScriptMethod()> _
  Public Shared Function GetPizza(ByVal contextKey As StringAs String
    Return String.Format("<span style='font-family:courier new;font-
     weight:bold;'>{0}</span>", "You Selected Pizza " + contextKey)
  End Function
</script>

17. Your code should be working, go and experiment with the solution

Explanation

The code presented here combines JavaScript with the use of web services. I think it deserves more clarification.

Listing 1 is really simple for anyone who has used html. It is simply creating a group of checks using the same name properties with different ID's. The trick here is the onclick event attached to the check box. Once a checkbox is clicked, the JavaScript function UpdatePizza gets called with a number of checkboxes to check.

Listing 2 shows the real JavaScript that reads all check boxes and formulates them into one string by concatenating the values of the checkboxes. However, there is an additional line to find the dynamic populate extender and call its populate method (dp1). You note that listing 3 specified that the behaviorid is dp1 (voila, the link).

Listing 3 brings about the whole picture. The targetcontrolid specified the panel that is going to be filled when the job is finished. Behaviorid is already explained. The option is the service method that is used to handle the dynamic call. GetPizza is the one called when the populate event is called from the JavaScript.

GetPizza webservice simply creates an HTML format filling for the panel using a sentence and a value passed from the java function.

Downloads
Conclusion

Dynamic populate extender is very useful when you intended to put a whole page alone and avoid using the update panel. It allows you as a developer to create a whole panel from scratch using simply checkboxes or even radio buttons.

Hope you enjoyed the article. Happy Ajaxfying and see you in the next article.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 4:33:53 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search