0

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"; } } 
2
  • 1
    Maybe I'm missing something, but if a second click on Button1 is supposed to undo the changes to Button2, shouldn't you have code that changes Button2 back the way it was? Commented Dec 17, 2012 at 6:07
  • Perhaps, but I don't know how! Commented Dec 18, 2012 at 1:40

1 Answer 1

1

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:

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

Updating an UpdatePanel has 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 UpdatePanel expert, but I think that adding this code to Button1_Click (that's a full postback, yes?) should get you going again.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, I couldn't make it work. I tried it before and after my existing code, then put it into its own IF statement testing to see if Button2 was Enabled as you suggested above with an ELSE being the existing code and when I click the button, nothing happens. Button text doesn't change and the page doesn't refresh. I did reset its properties as you suggested too just in case and that didn't work either. I edited my main post to reflect what I have tried.
I got it working. It was because I had disabled the button. As soon as I took that away, it worked as expected. So since the button was disabled, the server ignored any clicks on it regardless of the OnClick in the code behind. Thanks for the assistance!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.