Dynamic content made easy *redux* [How to: Use the new dynamic population support for Toolkit controls]

This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments (23)
  1. leomrozek says:

    I tried the above and it works great if you are on a single aspx page. However, If I nest the code into a Web User Control and drop onto a web page that uses a Master Web Page the Web Method is never executed. Any suggestions on how to make this scenario work? Are there scripts I need to move/update for this scenario?

    I am using other AJAX Controls on this page like the CollapsiblePanelExtender, UpdateProgress and UpdatePanels and they are working fine, its just the WebMethod is never called from the ModalPopupExtender.

  2. David Anson says:


    The problem seems to come about when you insert the sample code into a WebUserControl. When the attempt is made to call the GetContent method to populate the dynamic content, the call is made relative to the page hosting the WebUserControl. However, the GetContent method isn’t defined there, and the relevant logic to connect the caller to the method doesn’t seem to be done by ASP.NET AJAX. To address this issue, you can simply move the GetContent implementation out to the page hosting the WebUserControl. Alternatively, make it a WebService and then you can host it and call it from anywhere you want!

  3. Delay's Blog says:

    I previously blogged an update to my sample demonstrating how to use the AJAX Control Toolkit’s dynamic

  4. Steward5732 says:

    How about if I want DataGrid or DataView inside Panel1 which value depends on DynamicContextKey?

  5. David Anson says:


    Something like what ScottGu blogged about in the following post should work for you:


    We’re hoping to incorporate a method of rendering ASP.NET controls for dynamic content into the Toolkit in a future release in order to make this easy for everyone. For now, ScottGu’s blog has helped a number of people out, so please give it a try!

  6. Hi all, I have installed ASP.NET AJAX 1.0 (the 01/23/2007 release) I have a usercontrol with a Panel

  7. jf25 says:

    I am getting an javascript "unknown runtime error" occurring on the below method.

    The error is on "e.innterHMTL = results" line .

    Has anyone else had this problem?


    _onMethodComplete : function (result, userContext, methodName) {

           // ignore if it’s not the current call.

           if (userContext != this._currentCallID) return;

           // Time has passed; make sure the element is still accessible

           var e = this.get_element();

           if (e) {

               e.innerHTML = result;




  8. David Anson says:


    I’ve seen IE give errors like this when attempting to write to the innerHTML property of an INPUT element. Maybe check that the DynamicControlID is pointing to something like an asp:Label instead of an asp:TextBox?

  9. ric_castagna says:

    OK…I’ve got a variation on a theme here, and I’m starting to beat my head against the wall.

    I can see from your example, you’re able to set the DynamicContextKey with the Eval statement because you’ve got the databinding going on in the same context.

    What I’ve got is a cascading dropdown list which is populated from external web service calls. This effectively removes the "local" databinding that you enjoy in your example.

    So, I’ve been trying to get the value out of the dropdown list but for the life of me can’t get it to select just the value of the appropriate dropdown.

    Can you help shed some light on this? I know it’s probably not 100% a modalpopup problem, but it’s starting to get really annoying! 🙂

    I’ve tried all the things I know to try:

    DynamicContextKey="<%# Eval(ddlLessons.SelectedValue.ToString()) %>"

    DynamicContextKey = <%= ddlLessons.SelectedValue %>


    Of course, when I pass in a plain ‘ol string value of "Test" the WebService receives the value of "Test" as the parameter. I just need to make it Dynamic now.

    Thanks for the help!


  10. David Anson says:


    The <%=/<%# syntax you’re using in your example is evaluated on the server at page render time when creating the HTML for the web page. It sounds like you’re trying to use that syntax to set DynamicContextKey on the client to whatever value is chosen in a DropDownList control that doesn’t postback. If so, I don’t think that’ll work since the server doesn’t get involved and has no opportunity to render out the desired value.

  11. ric_castagna says:


    That makes a lot of sense, actually…so, have you found a way to make that DynamicContextKey something that can be set from a client-side function or in response to the OnSelectedIndexChanged event of the dropdown(s) that are already being ‘watched’ by an update panel?

    I’d hate to have to scrap this entire idea, but populating the information and tracking the value is necessary if I’m going to be able to use this for modify/delete operations.

    I appreciate the help!


  12. David Anson says:


    ModalPopupBehavior exposes a set_DynamicContextKey method (defined by DynamicPopulateBehaviorBase in BaseScripts.js) that you can use for this purpose. However, because the populate method in DynamicPopulateBehaviorBase caches the DynamicPopulateBehavior instance it creates, that technique will only affect the first invocation of ModalPopup. If that’s not a problem for your scenario, great!

    But if you need to change the value subsequently, you could examine the _dynamicPopulateBehavior member of your ModalPopupBehavior instance to verify the internal DynamicPopulateBehavior has been created and then call set_ContextKey on that instance to update the value it uses. This technique should work fine, though please note that _dynamicPopulateBehavior is officially "private", so this method could possibly break in a future release (though I doubt it will).

  13. ric_castagna says:

    OK, I must be too dumb to understand how to implement the set_DynamicContextKey method you’ve talked about.

    Where can I wire this up? I’ve tried attaching it to the "OnClientClick" property of the linkbutton that is supposed to fire my modalpopup, but it just hangs the browser. Odd discovery, when I put simple javascript such as alert("Hello") into that OnClientClick property it works just as expected…it’s only when I try to call a javascript function that the browser hangs.

    I know that Davaus was trying to do this on another thread of your blog, but unfortunately for me, he didn’t show how to call the javascript function either.

    I really appreciate and need some help on this one!


  14. David Anson says:


    The sample code below is derived from the sample from this post (which is available as ToolkitTestsManualDataboundDynamicPopulate.aspx in Toolkit releases). I’ve simplified the code and modified it to demonstrate the use of set_DynamicContextKey as discussed above. To see it in action, view the page, click "Popup", note the text "Original", dismiss the popup, click "Change", click "Popup", and note the updated text "Changed". This text comes from the ModalPopup’s contextKey and the "Change" button uses set_DynamicContextKey to make the switch.

    Hope this helps!

    <%@ Page Language="C#" %>

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

    <script runat="server">



     public static string GetContent(string contextKey)


       return contextKey;



    <html xmlns="http://www.w3.org/1999/xhtml"&gt;

    <head id="Head1" runat="server">

     <title>ModalPopup set_DynamicContextKey Demonstration</title>

     <style type="text/css">

       body { font-family:Verdana;     font-size:10pt; }

       .background { background-color:Gray; }

       .popup { width:200px;             padding:10px;        background-color:White;

                border-style:solid;      border-color:Black;  border-width:2px;

                vertical-align: middle;  text-align:center; }




     <form id="form1" runat="server">

       <asp:ScriptManager ID="ScriptManager1" runat="server" />

       <asp:LinkButton ID="LinkButton" runat="server" Text=’Popup’ />

       <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender" runat="server"

         TargetControlID="LinkButton" PopupControlID="Panel1" OkControlID="Button1"

         BackgroundCssClass="background" DynamicControlID="Label1"

         DynamicServiceMethod="GetContent" DynamicContextKey="Original"

         BehaviorID="ModalPopupBehavior" />

       <asp:Button ID="Button" runat="server" Text="Change" UseSubmitBehavior="true"

         OnClientClick="$find(‘ModalPopupBehavior’).set_DynamicContextKey(‘Changed’); return false;" />

       <asp:Panel ID="Panel1" runat="server" CssClass="popup" style="display:none;">

         <p><asp:Label ID="Label1" runat="server" /></p>

         <p><asp:Button ID="Button1" runat="server" Text="OK" /></p>





  15. ric_castagna says:

    Dude…you rock! Works like a champ. I never would’ve thought to chain javascript into the OnClientClick event…I kept looking for a way to call a single function and, as we saw, that wasn’t doing me a bit of good.

    Thanks for the help!


  16. hi I would like to dynamically populate a datalist on a panel that will be popped up by my HoverExtender.

  17. Mehdi_LA says:

    Is it possible to use the ModalPopupExtender to simply display a wait panel with no OK/Cancel button on it while a web service call is being made and then remove panel (modal popup) once the web server returns the results (and display the results on the page)?

  18. David Anson says:


    Yes, this should be pretty easy! All you need to do in JavaScript on the client side is to find the ModalPopupBehavior (use $find and specify the value of the ModalPopupExtender’s BehaviorID property), call the behavior’s .show() method before calling your web service to display the popup, and then call the .hide() method after the web service completes (successfully or unsuccessfully). It’s that easy! 🙂

  19. verrrierRoberts says:

    Setting the DynamicContextKey on the client side – One solutio.

    I have seen the methods to set the DynamicContextKey at dynamically at databinding using set_DynamicContextControl, this does not work for me, though setting _DynamicContextKey does work. This means you can have one modalPOpUpExtender driven off the entries in at table set on an onclick event roughly set up by

       popper = $find(‘yourmodalPopupExtender’);

       popper._DynamicContextKey = ctxval;


    This is based on an idea from Damian Meher’s blog at


    It seems you have to set the DynamicControlID to a div tag you want to replace which leaves the OKButton in place.

    Jonathan Roberts

  20. joergbauer says:


    thanks for your nice code. Everything works fine.

    When I upload this on my server and set in my web.config sessionState to cookieless="true"

    I get this error message: Web Service call failed: 500

    What can I do now? Is there any solutions for this?

    Thanks for any help!


  21. David Anson says:


    When you set configuration/system.web/sessionState[cookieless="true"], ASP.NET stores its SessionID in the URL instead of in a cookie. The Toolkit’s ServicePathConverter (ExtenderBaseServicePathConverter.cs] doesn’t preserve the SessionID part of the URL for page method web services (i.e., web services where the ServicePath is not set and the method lives in the same page), so a System.InvalidOperationException results with the message "Authentication failed." and the web service call fails as you report. I was able to fix this on my machine by changing the ServicePathConverter’s use of currentContext.Request.FilePath to currentContext.Request.RawUrl on line 27 of ServicePathConverter.cs. I’ve just created work item 10262 [http://www.codeplex.com/AtlasControlToolkit/WorkItem/View.aspx?WorkItemId=10262] to address this in a future release of the Toolkit.

    Thanks for reporting this!

  22. joergbauer says:


    I changed it to currentContext.Request.RawUrl and it worked but if the URL has a query like http://mydomain.com?query=xxx it does not work.

    Do you have a solution for this new problem?

    Thanks for your help!


  23. David Anson says:


    Indeed I have a new solution. 🙂 Well, I guess it’s more of a revised solution… Like before, we want to get at the URL with the SessionID in it – except this time the implementation’s a little more involved. The RawUrl property was nice for having the SessionID in it, but didn’t work in the more complicated scenario because it also had the query string in it. So what we want is to get access to a Uri object based on RawUrl so we can use the methods of the Uri class to strip off the query string properly. With this alternate approach the line of code (same location as before) looks like:

    return (new Uri(currentContext.Request.Url, currentContext.Request.RawUrl)).AbsolutePath;

    I’ve this out and it seems to work with/without a query string. I hope it works as well for you!

Comments are closed.