【问题标题】:Control event not firing from within updatepanel控制事件未从更新面板中触发
【发布时间】:2013-03-21 05:17:08
【问题描述】:

我有一个列表框,它正在通过计时器更新并在 UpdatePanel 中按预期工作。

但是我无法触发 selectedindexchanged 事件。我认为这与部分回发有关。有人知道我可以做些什么来完成这项工作吗?

当我将它移出 UpdatePanel 时,它工作正常。但是显然我不能做部分回发。

<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
    <asp:ListBox ID="ListBox_JobPositions" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged"  runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>

更新:

现在已经尝试了以下更改,计时器事件仍在工作,但 selectedindexchanged 事件没有。我迷路了。

<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
    <asp:ListBox ID="ListBox_JobPositions" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>

当列表框位于 UpdatePanel 内时,该事件不会触发,但不在时会起作用。

protected void ListBox_JobPositions_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("test");
    }

【问题讨论】:

  • 向我们展示更多的标记代码?
  • 这基本上就是整个程序。正如你所看到的,它并不多。

标签: asp.net ajax


【解决方案1】:

您没有收到事件的原因是,您的更改事件没有导致回发。您的回发是由计时器引起的。

asp.net 接收到的事件是定时器事件,而不是 ListBox 事件。

要解决此问题,您应该将 AutoPostBack 设置为 true。这将导致 ListBox 在数据更改并且您的事件应该触发时立即执行 PostBack。

<asp:ListBox ID="ListBox_JobPositions" AutoPostBack="True"  
     OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged"  
     runat="server" Height="750px" Width="300px" 
     DataSourceID="sqlDataSource" 
     DataTextField="Company" 
     DataValueField="Pid">
</asp:ListBox>

由于您已将UpdateMode 设置为Conditional,您还应该将ChildrenAsTriggers 设置为true。这样 List 会导致 PostBack,这也将是部分更新。

<asp:UpdatePanel ID="UpdatePanel" runat="server" 
                 UpdateMode="Conditional" 
                 ChildrenAsTriggers="True">

【讨论】:

  • 我已启用 Autopostback 并将 Updatemode 更改为 always 并且 Childrenastriggers 设置为 true 但它仍然无法正常工作。
  • 尝试增加你的调试时间间隔。 500 毫秒是您在计时器触发之前更改元素的非常短的持续时间。
  • 增加计时器也没有帮助。事件仍未触发。
【解决方案2】:

现在可以工作了,必须手动指定异步和完整回发触发器。感谢您的帮助。

<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
    <asp:ListBox ID="ListBox_JobPositions" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" />
    <asp:PostBackTrigger ControlID="ListBox_JobPositions" /> 
</Triggers>
</asp:UpdatePanel>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多