执行经过的事件回调后,标签文本值不会更改

时间:2016-07-15 04:34:24

标签: c# asp.net timer

大家好我还是c#事件和计时器的新手,但似乎我有一个让我感到困惑的错误,尽管看似在线工作代码。我有一个简单的搜索功能,在计时器结束后触发。在该函数触发之前,我将结果标题设置为“正在搜索...”,并且在该过程结束时,我希望它更改为“1152 results found”。但是标签不会改变,尽管在调试中我点击代码设置它,我甚至看到searchresultTitle.Text值被更改,“list”实际上包含1152项。该网站没有反映它,我设置计时器的方式有什么问题,或者我错过了什么?

protected void StartSearchClick(object sender, EventArgs ev)
{
    String textVal = Request["SearchBox"];
    textVal = textVal.Replace('*', '_');//to support * as wildcard
    String publicChoice = PublicChoice.SelectedValue;
    int iChoiceVal = 1;
    Int32.TryParse(publicChoice, out iChoiceVal);

    SearchResultTitle.Text = "Search in progress...";
    System.Timers.Timer aTimer = new System.Timers.Timer(1000);
    aTimer.Elapsed += (s, e) => ReadPublishedLessons(textVal, iChoiceVal);
    aTimer.AutoReset = false;
    aTimer.Start();
}

private void ReadPublishedLessons(string namePart, int iPublic)
{
    //null check
    if (namePart == null)
        return;

    eon.LessonInfo[] list = WsAdmin.GetLessonList(namePart, iPublic);
    SearchResultTitle.Text = list.Length + " results found";
}

1 个答案:

答案 0 :(得分:1)

Bharadwaj在评论中...

对于asp.net,仅在回发或部分回发后,html才会发送回客户端。在事件触发器调用结束时,已经准备就绪的html已通过搜索正在进行中...消息到达客户端。但是,当您尝试更改label的值时,该值正在更改,并且仅在服务器上。 –巴拉德瓦伊16年7月15日,下午4:45