<%@ Page Language="C#" AutoEventWireup="true" Title="DynamicPopulate Sample"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> <script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string GetHtml(string contextKey)
{
// A little pause to mimic a latent call
System.Threading.Thread.Sleep(250); string value = (contextKey == "U") ?
DateTime.UtcNow.ToString() :
String.Format("{0:" + contextKey + "}", DateTime.Now);
return String.Format("<span style='font-family:courier new;font-weight:bold;'>{0}</span>", value);
}
</script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Dynamic Populate 2</title>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager runat="Server" ID="ScriptManager1" />
<script type="text/javascript">
function updateDateKey(value) {
var behavior = $find('dp1');
if (behavior) {
behavior.populate(value);
}
}
Sys.Application.add_load(function () { updateDateKey('G'); });
</script>
<div class="demoarea">
<p>
<asp:Label ID="Label1" runat="server" Text="Label" CssClass="subheading">Time at the server:</asp:Label>
</p>
<p>Choose a date/time format:</p>
<p>
<label for="r0">
<input type="radio" name="rbFormat" id="r0" value='G'
onclick="updateDateKey(this.value);" checked="checked" />Normal
</label>
<br />
<label for="r1">
<input type="radio" name="rbFormat" id="r1" value='d'
onclick="updateDateKey(this.value);" />
Short Date
</label>
<br />
<label for="r2">
<input type="radio" name="rbFormat" id="r2" value='D'
onclick="updateDateKey(this.value);" />Long Date
</label>
<br />
<label for="r3">
<input type="radio" name="rbFormat" id="r3" value='U'
onclick="updateDateKey(this.value);" />UTC Date/Time
</label>
<br />
</p>
<br />
<asp:Panel ID="Panel1" runat="server" CssClass="dynamicPopulate_Normal" />
<ajaxToolkit:DynamicPopulateExtender ID="dp1"
runat="server"
TargetControlID="Panel1"
PopulateTriggerControlID="Label1"
ServiceMethod="GetHtml" />
</div>
</form>
</body>
</html>
要特别注意添加DynamicPopulateExtender的PopulateTriggerControlID 属性。 如果没有添加PopulateTriggerControlID 属性,页面会一直报错——
Can't find control with ID ''。
”