Stopping auto page refresh - c#

I use the below code to auto refresh the page every 60 seconds via the AJAX tools in VS2010. Works perfectly.
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled" UpdateMode="Conditional">
<ContentTemplate>
ASP.NET/HTML Code
<p>
<asp:Button ID="Button2" runat="server" Text="Click here" OnClick="Button2_Click" /> to disable the pages automatic refresh.</p>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="60000">
</asp:Timer>
</asp:View>
<asp:View ID="View2" runat="server">
etc.
</asp:MultiView>
I want to include a button on the asp.net page to cancel the auto refresh.
I tried to include the below but when I clicked the button, it didn't work. The below is the Code Behind for an OnClick event for a Button. The asp.net code is in the above code.
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Interval = 0;
}
Where am I going wrong? Is this even a way to do this or do I need to go another route in order to allow the user to cancel the auto page refresh?

Thanks to PeterJ I have found the solution. I modified the code and since I clicked it the page has not refreshed. The issue was with my code behind for the button OnClick event. I had:
Timer1.Interval = 0;
When I should have had:
Timer1.Enabled = false;

Related

AsyncPostBackTrigger for Checkbox control not firing

Context of the problem:
I have a checkbox and a button. The button is disabled and colored grey upon the page load. I want the checkbox to enable and re-color the button if checked, and revert the change if unchecked. Think of it as a User Agreement page, where the user accepts the terms and needs to click on the checkbox to proceed.
There is a grid view and other components above the page that would break upon a postback, so searching online I found that I could use an UpdatePanel. Thought this would be more simple than writing jQuery for the checkbox, but it isn't working
Code:
ASPX
<asp:UpdatePanel ID ="upCheckbox" runat="server">
<ContentTemplate>
<asp:CheckBox ID="checkLabel" runat="server" OnCheckedChanged="checkLabel_CheckedChanged"/><asp:Label ID="AknowledgementLabel" runat="server" Text="Info is correct & Enable button"></asp:Label>
<br /><br />
<asp:Button ID="btnSubmit" Text="Submit Application" CssClass="jqbutton" OnClick="btnSubmit_Click" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="checkLabel" EventName="CheckedChanged"/>
</Triggers>
</asp:UpdatePanel>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
btnSubmit.Enabled = false;
btnSubmit.ForeColor = System.Drawing.Color.Gray;
}
protected void checkLabel_CheckedChanged(object sender, EventArgs e)
{
if (checkLabel.Checked)
{
btnSubmit.Enabled = true;
btnSubmit.ForeColor = System.Drawing.ColorTranslator.FromHtml("#336699");
}
else
{
btnSubmit.Enabled = false;
btnSubmit.ForeColor = System.Drawing.Color.Gray;
}
}
Problem:
The checkbox does not work and the submit button remains disabled. Not sure if I am using Update Panels for the appropriate purpose or if am missing something. Is it possible to accomplish this the way that I am trying to, or should I move on to jQuery?
You are missing AutoPostback="true" in your CheckBox control. By default it is set to false. With autopostback missing your event won't reach server side.
<asp:UpdatePanel ID ="upCheckbox" runat="server">
<ContentTemplate>
<asp:CheckBox ID="checkLabel" runat="server" AutoPostback="true" OnCheckedChanged="checkLabel_CheckedChanged"/>
<asp:Label ID="AknowledgementLabel" runat="server" Text="Info is correct & Enable button"></asp:Label>
<br /><br />
<asp:Button ID="btnSubmit" Text="Submit Application" CssClass="jqbutton" OnClick="btnSubmit_Click" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="checkLabel" EventName="CheckedChanged"/>
</Triggers>
</asp:UpdatePanel>

ASP C# UpdatePanel does not update contents

I am trying to update the datasource of a webdatagrid in an updatepanel. I am able to do this via a timer tick event
<asp:timer id="Timer1" runat="server" interval="10000" ontick="Timer1_Tick"></asp:timer>
<asp:updatepanel id="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
</Triggers>
<ContentTemplate>
<ig:WebDataGrid ID="WebDataGrid" runat="server" AutoGenerateColumns="false" >
</ig:WebDataGrid>
</ContentTemplate>
</asp:updatepanel>
And with the Tick event handler
protected void Timer1_Tick(object sender, EventArgs e)
{
WebDataGrid.DataSource = *a datatable*;
WebDataGrid.DataBind();
}
However, now when I try to do the same thing except instead of using the timer, use the WebDataGridMain_RowSelectionChanged event instead the webdatagrid does not update. I believe the new datasource value is still being sent to the client however the UpdatePanel does not seem to be updating the view.
<ig:WebDataGrid ID="WebDataGridMain" runat="server" AutoGenerateColumns="False" OnRowSelectionChanged="WebDataGridMain_RowSelectionChanged">
<ClientEvents AJAXResponse="AJAXResponseHandler" />
</ig:WebDataGrid>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="WebDataGridMain" EventName="RowSelectionChanged"/>
</Triggers>
<ContentTemplate>
<ig:WebDataGrid ID="WebDataGridDetail" runat="server" AutoGenerateColumns="false">
</ig:WebDataGrid>
</ContentTemplate>
</asp:UpdatePanel>
With the the similar event handler code below
protected void WebDataGridMain_RowSelectionChanged(object sender, Infragistics.Web.UI.GridControls.SelectedRowEventArgs e)
{
WebDataGridDetail.DataSource = *a datatable*;
WebDataGridDetail.DataBind();
}
There doesn't seem to be any difference between these two except for the type of event trigger. What should I change so that the UpdatePanel works with the RowSelectionChanged event?

textBox Lose focus on nested Gridview

I have two nested Gridviews, the parent is for Posts and child is for Comments. I want to update Post and Comments with a Time Interval so Im using an asp.net Timer. My problem is that a TextBox that is in the first Gridview loses focus when timer Ticks. I searched the web a lot, one possible solution was to take the textbox out of the UpdatePanel but in this situation I can't take out the textBox. Please help me, here is my code.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" Interval="10000" OnTick="Timer1_Tick" runat="server">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<%--post GridView--%>
<asp:GridView ID="posts" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%--Comments Gridview--%>
<asp:GridView ID="comments" runat="server"></asp:GridView>
<%--a Textbox and bUtton For sending new Comment--%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{
posts.DataSource = GetData();
posts.DataBind();
}
I was going to type a similar answer to this, but it appears it has already been explained here - https://stackoverflow.com/a/22014420/2248290
The key point being: "An update panel takes focus away as it posts back"

button OnClick event doesn't fire at the first time in UpdatePanel

I have an issue on button OnClick event.
There is a refresh button in a user control ("header") which will refresh a place holder ("phContent") that generates some other user controls at run time. However, the button OnClick event doesn't fire until the whole place holder content loads.
Page.aspx
<ext:ContentHeader ID="header" runat="server" Visible="false" />
<asp:UpdatePanel ID="upControl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:PlaceHolder ID="phContent" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="header" EventName="OnFormSubmit" />
</Triggers>
</asp:UpdatePanel>
UserControl.ascx
<div>
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_OnClick" />
</div>
UserControl.cs
protected void btnRefresh_OnClick(object sender, EventArgs e)
{
//some code
OnFormSubmit(this, e);
}
public delegate void UserControlFormSubmit(object sender, EventArgs e);
public event UserControlFormSubmit OnFormSubmit;
Not sure what exactly the page and source code is.
But from front-end, take for jQuery as example.
You can try something like:
jQuery(function(){
jQuery('#btnRefresh').on('click',function(){
//load the content by ajax
jQuery('#phContent').load('#chart');
});
});
You have to place the button in updatepanel as well.
<ext:ContentHeader ID="header" runat="server" Visible="false" />
<asp:UpdatePanel ID="upControl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_OnClick" />
</ContentTemplate>
</asp:UpdatePanel>

UpdatePanel not firing inside Wizard

I have an Update panel within a wizard:
<asp:WizardStep ID="WizardStep2" runat="server" StepType="Auto"
Title="Set the number of users required.">
...
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Always" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="ProgressInd" Text="Progress..." />
<asp:Button runat="server" OnClick="GoButton_Click" ID="ProgressBtn" Text="Go" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:WizardStep>
...
protected void GoButton_Click(object sender, EventArgs e)
{
ProgressInd.Text = "Progress... Moving";
}
When I take the update panel out of the wizard it works nicely but inside the wizard the click event just won't fire. I'm using Firefox to test, but IE doesn't work either. Any ideas or help appreciated.
For the record. Paolo spotted my problem. There were page validators that were preventing the event from firing.

Categories