2013-10-28 32 views
1

我正在使用updatepanel和一些触发器实现一个页面。ASP.NET多触发器无法正常工作

我想要实现的是以下。

  1. 计时器(timer1)以10秒为间隔更新UpdatePanel(update_content)。

  2. 如果用户手柄单选按钮列表(rbl_axis),列表框(list_point), 立即更新的UpdatePanel。

  3. 全部更新发生在异步

这是我的代码。

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

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

<script runat="server" type="text/C#"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      div_title.InnerText= "Hello"; 

      ScriptManager1.RegisterAsyncPostBackControl(rbl_axis); 
      ScriptManager1.RegisterAsyncPostBackControl(list_point); 
      printTime("Page_Load"); 

     } 
    } 

    protected void printTime(string message) 
    { 
     div_content.InnerHtml += message +": "+ DateTime.Now.ToLongTimeString() + "<br />"; 
    } 

    protected void Timer1_Tick(object sender, EventArgs e) 
    { 
     printTime("<font color='red'>Timer</font>"); 
    } 

    protected void rbl_axis_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     printTime("Axis("+rbl_axis.SelectedValue+")"); 
    } 

    protected void list_point_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     printTime(list_point.SelectedValue); 
    } 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
    <div> 
     <form id="form1" runat="server"> 

      <asp:RadioButtonList ID="rbl_axis" runat="server" RepeatDirection="Horizontal" 
       OnSelectedIndexChanged="rbl_axis_SelectedIndexChanged"> 
       <asp:ListItem Text="X" Value="X" Selected="True"></asp:ListItem> 
       <asp:ListItem Text="Y" Value="Y" ></asp:ListItem> 
       <asp:ListItem Text="Z" Value="Z" ></asp:ListItem> 
      </asp:RadioButtonList> 

      <asp:ListBox ID="list_point" runat="server" SelectionMode="Multiple" Width="100px" Height="100px" 
       OnSelectedIndexChanged="list_point_SelectedIndexChanged"> 
       <asp:ListItem Text="Point1" Value="Point1"></asp:ListItem> 
       <asp:ListItem Text="Point2" Value="Point2"></asp:ListItem> 
       <asp:ListItem Text="Point3" Value="Point3"></asp:ListItem> 
       <asp:ListItem Text="Point4" Value="Point4"></asp:ListItem> 
       <asp:ListItem Text="Point5" Value="Point5"></asp:ListItem> 
      </asp:ListBox> 

      <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
      <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="10000"></asp:Timer> 

      <asp:UpdatePanel ID="update_content" runat="server"> 
       <Triggers> 
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> 
       </Triggers> 

       <ContentTemplate> 
        <div id="div_title" runat="server"></div> 
        <div id="div_content" runat="server"></div> 
       </ContentTemplate> 

      </asp:UpdatePanel> 
     </form> 
    </div> 
</body> 
</html> 

它没有正常工作。

更新面板只更新定时器。

如果我处理列表框和单选按钮控件,更新面板不会更新。

并在下一个计时器滴答声,列表框和单选按钮控件的修改应用。

我该如何实现我想要的。

你能给我一些建议吗?

预先感谢您。

回答

1

您的RadioButtonList和ListBox必须将AutoPostBack属性设置为true才能触发回发。

此外,您还需要将这些回发事件注册到UpdatePanel,这几乎与您使用Timer Click事件相同。

例如,你的代码可能会更喜欢这样的:

<asp:RadioButtonList ID="rbl_axis" runat="server" RepeatDirection="Horizontal" 
    OnSelectedIndexChanged="rbl_axis_SelectedIndexChanged" AutoPostBack="true"> 
    <asp:ListItem Text="X" Value="X" Selected="True"></asp:ListItem> 
    <asp:ListItem Text="Y" Value="Y" ></asp:ListItem> 
    <asp:ListItem Text="Z" Value="Z" ></asp:ListItem> 
</asp:RadioButtonList> 
<asp:ListBox ID="list_point" runat="server" SelectionMode="Multiple" Width="100px" 
    Height="100px" OnSelectedIndexChanged="list_point_SelectedIndexChanged" 
    AutoPostBack="true"> 
    <asp:ListItem Text="Point1" Value="Point1"></asp:ListItem> 
    <asp:ListItem Text="Point2" Value="Point2"></asp:ListItem> 
    <asp:ListItem Text="Point3" Value="Point3"></asp:ListItem> 
    <asp:ListItem Text="Point4" Value="Point4"></asp:ListItem> 
    <asp:ListItem Text="Point5" Value="Point5"></asp:ListItem> 
</asp:ListBox> 
<asp:UpdatePanel ID="update_content" runat="server"> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> 
     <asp:AsyncPostBackTrigger ControlID="rbl_axis" EventName="SelectedIndexChanged" /> 
     <asp:AsyncPostBackTrigger ControlID="list_point" EventName="SelectedIndexChanged" /> 
    </Triggers> 
    <ContentTemplate> 
     <div id="div_title" runat="server"></div> 
     <div id="div_content" runat="server"></div> 
    </ContentTemplate> 
</asp:UpdatePanel> 
+0

我真的很感谢你的帮助。它运作良好。再次感谢你 – user2423434