I use a Multiview to display some Charts. On first load, the page presents 2 drop downs and a GO button. I use the Code Behind to choose which View to display based on the selections from the dropdowns.
The page has an AJAX refresh with a Button that allows the user to stop auto-refreshing. The button disables the Timer, changes the button text, Italicises it and disables the button itself. This works great.
The user selects their options and hits GO and the requested View displays. I have not allowed for a way to turn on auto refresh. Purely out of ease for myself. I thought the easiest way would be to just allow the user to hit the GO button again (The selections governing the current View are still selected in the dropdowns) which would reload the View with the auto-refresh enabled by default.
Below is the essential code for what I currently have with just one view to demonstrate.
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>
CODE BEHIND
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;
}
My issue is that when I click the GO button, the updated button conditions are still in place. ie. The name is changed, italicized and disabled. Is there a way to force a View to completely reload? I hope this makes sense. I tried UpdatePanel1.Update(); as can be seen above in the Button1_Click method but it didn’t work.
Using Ann L’s suggestion, I tried the following but none would work:
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";
}
}
To solve this issue I put the following in to my 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";
}
}
OK, assuming I understand what you’re asking (how to undo the changes made to
Button2), here is how I would do it:The following code would go into
Button1_Click:Updating an
UpdatePanelhas no effect if you haven’t changed the state of any controls. It’s not like refreshing an iframe or the whole page: it just re-renders the page, making changes visible. It doesn’t reset the values or properties of anything.I am not an
UpdatePanelexpert, but I think that adding this code toButton1_Click(that’s a full postback, yes?) should get you going again.