我使用 Multiview 来显示一些图表。首次加载时,页面显示 2 个下拉菜单和一个 GO 按钮。我使用后面的代码根据下拉菜单中的选择来选择要显示的视图。
该页面有一个 AJAX 刷新,带有一个允许用户停止自动刷新的按钮。该按钮禁用计时器,更改按钮文本,将其斜体并禁用按钮本身。这很好用。
用户选择他们的选项并点击 GO 并显示请求的视图。我不允许打开自动刷新的方法。纯粹是为了我自己。我认为最简单的方法是只允许用户再次点击 GO 按钮(管理当前视图的选择仍然在下拉列表中选择),这将在默认情况下启用自动刷新重新加载视图。
下面是我目前只有一个视图来演示的基本代码。
ASP.NET
<asp:Content ID="HeadContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
function SetText(id) {
if (id.value == "Disable automatic page refresh")
id.value = "Processing Request ...";
}
</script>
</asp:Content>
<asp:DropDownList ID="itemDropdown" runat="server">
ASP LIST ITEMS
</asp:DropDownList>
<asp:DropDownList ID="timeDropdown" runat="server">
ASP LIST ITEMS
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Go" OnClick="Button1_Click" />
</p>
<p>
<asp:Label ID="errorLabel" runat="server" CssClass="errorLabel"></asp:Label>
</p>
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Disable automatic page refresh" OnClick="Button2_Click" OnClientClick="return SetText(this)" /></p>
CONTENT HERE
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="60000">
</asp:Timer>
</asp:View>
代码背后
protected void Button1_Click(object sender, EventArgs e)
{
if (errorLabel.Text != null)
{
errorLabel.Text = string.Empty;
}
if (itemDropdown.SelectedValue == "5" && timeDropdown.SelectedValue == "0.5")
{
MultiView1.ActiveViewIndex = 0;
UpdatePanel1.Update();
}
IF CONTINUES ...
else
{
errorLabel.Text = "You did not choose a valid Item or Timeframe. Please try again.";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Enabled = false;
Button2.Text = "Automatic Refresh Disabled";
Button2.Font.Italic = true;
Button2.Enabled = false;
}
我的问题是,当我单击 GO 按钮时,更新的按钮条件仍然存在。IE。名称已更改、斜体和禁用。有没有办法强制视图完全重新加载?我希望这是有道理的。我试过 UpdatePanel1.Update(); 如上面在 Button1_Click 方法中所见,但它不起作用。
使用 Ann L 的建议,我尝试了以下方法,但没有一个可行:
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Enabled = false;
Button2.Text = "Automatic Refresh Disabled";
Button2.Font.Italic = true;
Button2.Enabled = false;
Timer1.Enabled = true;
Timer1.Interval = 10000;
Button2.Text = "Disable automatic page refresh";
Button2.Font.Italic = false;
Button2.Enabled = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
Timer1.Interval = 10000;
Button2.Text = "Disable automatic page refresh";
Button2.Font.Italic = false;
Button2.Enabled = true;
Timer1.Enabled = false;
Button2.Text = "Automatic Refresh Disabled";
Button2.Font.Italic = true;
Button2.Enabled = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
if (!Button2.Enabled)
{
Timer1.Enabled = true;
Timer1.Interval = 10000;
Button2.Text = "Disable automatic page refresh";
Button2.Font.Italic = false;
Button2.Enabled = true;
}
else
{
Timer1.Enabled = false;
Button2.Text = "Automatic Refresh Disabled";
Button2.Font.Italic = true;
Button2.Enabled = false;
Button2.ToolTip = "Click again to resume automatic refresh";
}
}
为了解决这个问题,我将以下内容放入我的代码后面:
protected void Button2_Click(object sender, EventArgs e)
{
if (Button2.Text == "Disable automatic page refresh")
{
Timer1.Enabled = false;
Button2.Text = "Automatic Refresh Disabled";
Button2.Font.Italic = true;
Button2.ToolTip = "Click again to resume automatic refresh";
}
else
{
Timer1.Enabled = true;
Timer1.Interval = 10000;
Button2.Text = "Disable automatic page refresh";
Button2.Font.Italic = false;
Button2.ToolTip = "Click to disable automatic page refresh";
}
}