我有一个带有多个UpdatePanel的ASP.NET Web表单,我希望有一个计时器只触发其中一个UpdatePanel来更新。然后,我想要一个单独的UpdatePanel来更新DropDownList的OnSelectedIndexChanged事件。我的代码将更新定时器触发的面板,但是当我更改索引时,下拉触发的面板将等到下一个计时器滴答更新。如何立即更新下拉触发的面板?代码如下。
C#:
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "UpdatePanel1 refreshed at: " +
DateTime.Now.ToLongTimeString();
}
protected void dropDown_indexChange(object sender, EventArgs e)
{
Label2.Text = "UpdatePanel2 refreshed at: " +
DateTime.Now.ToLongTimeString();
//UpdatePanel2.Update(); -- Didn't help
}
}
ASP:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="not updated yet"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="3000" ontick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="also not updated yet"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<br />
<br />
<asp:DropDownList OnSelectedIndexChanged="dropDown_indexChange" ID="DropDownList1" runat="server">
<asp:ListItem>This</asp:ListItem>
<asp:ListItem>That</asp:ListItem>
<asp:ListItem>The Other</asp:ListItem>
</asp:DropDownList>
</div>
</form>
答案 0 :(得分:2)
将DropDownList移动到UpdatePanel并在其上设置.AutoPostBack = true
。