douxiongye5779 2016-01-18 07:42
浏览 74

通过Async方法和Task使用下载的数据填充ASP.NET控件

I am building an ASP.NET web application that loads data dynamically from a MySQL database. I already know that I can change the properties of client control from within an event on the server that is triggered by another client control (a button click, for example).

That said, I have been unable to change the controls in that same fashion after downloading my dynamic data from the database. I think this is because the client has already been rendered by the time my data has finished downloading. Is there any way to inform the client that it should reload the page to receive the freshly downloaded data? Maybe I can send the data directly to the client?

In a perfect world, the dynamic data would start downloading in Page_Load, like below, and automatically populate the control (a DropDown in this case) once the data has been received and formatted (at the end of DownloadSchoolsInfo). Is this possible?

Thanks for any suggestions!

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Task task = new Task(DownloadSchoolsInfo);
        task.Start();
    }
}

async void DownloadSchoolsInfo()
{
    Task<string> task = StaticData.DoStringDownload("http://php.domain.com/php/getAllSchoolInfos.php");
    string s = await task;
    string[] schoolData = s.Split('
');

    foreach (string line in schoolData)
    {
        string[] sArray = line.Split(';');
        StaticData.schools.Add(new SchoolInfo(sArray[0], sArray[1], sArray[2], sArray[3]));
    }

    dropDown_SchoolSelect.DataTextField = "name";
    dropDown_SchoolSelect.DataValueField = "code";
    dropDown_SchoolSelect.DataSource = StaticData.schools;
    dropDown_SchoolSelect.DataBind();
}

public static async Task<string> DoStringDownload(string url)
{
    var httpClient = new HttpClient();
    string s = await httpClient.GetStringAsync(url);
    return s;
}
  • 写回答

1条回答 默认 最新

  • douweng7233 2016-01-18 08:23
    关注

    Isn't this easilly done with DataContext and ObservableCollection?

    <ComboBox ItemsSource="{Binding Schools}" 
        SelectedItem="{Binding SelectedSchool}"
        />
    

    And somewhere in your model, you'd have:

    public class MainPageViewModel : BaseViewModel
    {
        private ObservableCollection<string> _schools;
        private string _selectedSchool;
    
        public ObservableCollection<string> Schools
        {
            get { return _schools; }
            set
            {                
                _schools = new ObservableCollection<string>(value);
                NotifyPropertyChange("Schools");
            }
        }
    
        public string SelectedSchool
        {
            get { return _selectedSchool; }
            set
            {
                if (_selectedSchool == value)
                    return;
    
                _selectedSchool = value;
                NotifyPropertyChange("SelectedSchool");
            }
        }
    }
    

    Now whenever you update the Schools collection, your dropdown is up to date, page_load or anything else. You can also force an update by calling the NotifyPropertyChange(string) method.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?