刷新View的内容

时间:2012-12-17 04:24:46

标签: c# asp.net

我使用Multiview来显示一些图表。首次加载时,页面会显示2个下拉菜单和一个GO按钮。我使用Code Behind根据下拉列表中的选择选择要显示的视图。

该页面有一个带有Button的AJAX刷新,允许用户停止自动刷新。该按钮禁用Timer,更改按钮文本,Italicises它并禁用按钮本身。这很有效。

用户选择他们的选项并点击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按钮时,更新的按钮条件仍然存在。即。名称已更改,斜体和禁用。有没有办法强制View完全重新加载?我希望这是有道理的。我试过UpdatePanel1.Update();可以在上面的Button1_Click方法中看到,但它没有用。

使用Ann的建议,我尝试了以下但没有一个可行:

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";
    }
}

为了解决这个问题,我将以下内容放入我的Code Behind:

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";
    }
}

1 个答案:

答案 0 :(得分: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无效。这不像刷新iframe或整个页面:它只是重新呈现页面,使更改可见。它不会重置任何值的值或属性。

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