0

我使用 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";
    }
}
4

1 回答 1

1

好的,假设我了解您的要求(如何撤消对 所做的更改Button2),我将这样做:

以下代码将进入Button1_Click

if(!Button2.Enabled)
{
    Timer1.Enabled = true;  // You might also have to reset its properties
    Button2.Text = "Disable automatic refresh";
    Button2.Font.Italic = false;
    Button2.Enabled = true;
}

UpdatePanel如果您没有更改任何控件的状态,则更新 an无效。这不像刷新 iframe 或整个页面:它只是重新呈现页面,使更改可见。它不会重置任何东西的值或属性。

我不是UpdatePanel专家,但我认为将这段代码添加到Button1_Click(这是一个完整的回发,是吗?)应该让你重新开始。

于 2012-12-18T13:38:19.677 回答