扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
public class WeatherDataSource : DataSourceControl { public static readonly string CurrentConditionsViewName = "CurrentConditions"; private WeatherDataSourceView _currentConditionsView; private WeatherDataSourceView CurrentConditionsView { get { if (_currentConditionsView == null) { _currentConditionsView = new WeatherDataSourceView(this, CurrentConditionsViewName); } return _currentConditionsView; } } public string ZipCode { get { string s = (string)ViewState["ZipCode"]; return (s != null) ? s : String.Empty; } set { if (String.Compare(value, ZipCode, StringComparison.Ordinal) != 0) { ViewState["ZipCode"] = value; CurrentConditionsView.RaiseChangedEvent(); } } } protected override DataSourceView GetView(string viewName) { if (String.IsNullOrEmpty(viewName) || (String.Compare(viewName, CurrentConditionsViewName, StringComparison.OrdinalIgnoreCase) == 0)) { return CurrentConditionsView; } throw new ArgumentOutOfRangeException("viewName"); } protected override ICollection GetViewNames() { return new string[] { CurrentConditionsViewName }; } public Weather GetWeather() { return CurrentConditionView.GetWeather(); } } |
private sealed class WeatherDataSourceView : DataSourceView { private WeatherDataSource _owner; public WeatherDataSourceView(WeatherDataSource owner, string viewName) : base(owner, viewName) { _owner = owner; } protected override IEnumerable ExecuteSelect( DataSourceSelectArguments arguments) { arguments.RaiseUnsupportedCapabilitiesError(this); Weather weatherObject = GetWeather(); return new Weather[] { weatherObject }; } internal Weather GetWeather() { string zipCode = _owner.ZipCode; if (zipCode.Length == 0) { throw new InvalidOperationException(); } WeatherService weatherService = new WeatherService(zipCode); return weatherService.GetWeather(); } internal void RaiseChangedEvent() { OnDataSourceViewChanged(EventArgs.Empty); } } |
Zip Code: <asp:TextBox runat="server" id="zipCodeTextBox" /> <asp:Button runat="server" onclick="OnLookupButtonClick" Text="查找" /> <hr /> <asp:FormView runat="server" DataSourceID="weatherDS"> <ItemTemplate> <asp:Label runat="server" Text='<%# Eval("Temperature", "当前温度是 {0}。") %>' /> </ItemTemplate> </asp:FormView> <nk:WeatherDataSource runat="server" id="weatherDS" ZipCode="98052" /> <script runat="server"> private void OnLookupButtonClick(object sender, EventArgs e) { weatherDS.ZipCode = zipCodeTextBox.Text.Trim(); } </script> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者