I wish to have 3 gridviews on a single aspx page fed by individual DB queries (displaying static data, no manipulation) and based on a 10 second timer, refresh the tables. I have the code to return the datatables sorted. I can make it update one gridview which is in one of my update panels, but the other two dont render.
Code is:
<%# Page Title="Index" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="Test.Admin" %>
<script runat="server" type="text/c#">
protected void Page_PreRender(object sender, EventArgs e)
{
Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
FullAccessSession.DataSource=GetStatus("FullAccess");
FullAccessSession.DataBind();
Label2.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
LimitedAccessSession.DataSource=GetStatus("LimitedStatus");
LimitedAccessSession.DataBind();
Label3.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
LogData.DataSource = GetLog() ;
LogData.DataBind();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
FullAccessSession.DataSource=GetStatus("FullAccess");
FullAccessSession.DataBind();
}
protected void Timer2_Tick(object sender, EventArgs e)
{
Label3.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
LogData.DataSource = GetLog() ;
LogData.DataBind();
}
protected void Timer3_Tick(object sender, EventArgs e)
{
Label2.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
LimitedAccessSession.DataSource=GetStatus("LimitedStatus");
LimitedAccessSession.DataBind();
}
</script>
<div class="row">
<div class="col-md-7">
<asp:UpdatePanel ID="UpdateFullAccessStatus" runat="server" UpdateMode="Always">
<ContentTemplate>
<!--<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>-->
<asp:Label ID="Label7" runat="server" Font-Bold="True" Text="Full Access Logged In Users"></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text="Panel not refreshed yet."></asp:Label>
<br />
<asp:GridView ID="FullAccessSession" runat="server">
</asp:GridView>
<br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdateLimitedAccessStatus" runat="server" UpdateMode="Always">
<ContentTemplate>
<!--<asp:Timer ID="Timer3" runat="server" Interval="10000" OnTick="Timer2_Tick">
</asp:Timer>-->
<asp:Label ID="Label4" runat="server" Font-Bold="True" Text="Limited Access Logged In Users"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Panel not refreshed yet."></asp:Label>
<br />
<asp:GridView ID="LimitedAccessSession" runat="server">
</asp:GridView>
<br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdateLog" runat="server" UpdateMode="Always">
<ContentTemplate>
<!--<asp:Timer ID="Timer2" runat="server" Interval="10000" OnTick="Timer2_Tick">
</asp:Timer>-->
<asp:Label ID="Label5" runat="server" Font-Bold="True" Text="General Log"></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text="Panel not refreshed yet."></asp:Label>
<asp:GridView ID="LogData" runat="server">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
I cant work out why one of my update panels are working. As you can see i've tried using the PreRender function as well as (currently commented out) timers. The labels are updating with the current time but only one gridview displays.
Any help would be appreciated
Thanks
The issue here is that the script of timer is lost after the post back inside the UpdatePanel - the solution is to take it out of the update panel and use Triggers
Here is an example that I test it and works.
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2800"></asp:Timer>
<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick" Interval="2500"></asp:Timer>
<div>
<div>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
<ContentTemplate>
<asp:Literal runat="server" ID="txtTest1"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div>
<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" />
</Triggers>
<ContentTemplate>
<asp:Literal runat="server" ID="txtTest2"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
and on code behind the ontick is the trigger, eg:
protected void Timer1_Tick(object sender, EventArgs e)
{
txtTest1.Text += "1.";
}
Related
I have a button in asp.net to clear textboxes and I used ajax as below:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
</ContentTemplate>
</asp:UpdatePanel>
As well, this button has following C# code:
protected void Button2_Click(object sender, EventArgs e)
{
txtFirstName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtEmail.Text = string.Empty;
txtSubject.Text = string.Empty;
txtMessage.Text = string.Empty;
}
However, foregoing C# code doesn't work when I execute this program!; In other words, textboxes don't clear after I click on button!
Please tell me why it happens?!
I have tried and the button code is working
.aspx code
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
.aspx.cs page coding
protected void Button2_Click(object sender, EventArgs e)
{
txtFirstName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtEmail.Text = string.Empty;
txtSubject.Text = string.Empty;
txtMessage.Text = string.Empty;
}
You need to modify your updatepanel.Kindly place all your Label and TextBox Control inside Update Panel and Button Events outside your UpdatePanel and add Trigger of your button ID.
AsyncPostBackTrigger or PostBackTrigger
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
// HERE YOUR TEXTBOX AND LABEL CONTROLS
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2"
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
You should place the Textboxes inside update panel as
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
</ContentTemplate>
</asp:UpdatePanel>
I have two panels. Each panel contains an updatepanel.
The first panel is a password Textbox.
I set the second panel's visibility on page_load to false.
If the user enters the correct password, the second panel should be visible and the first panel shouldnd.
The code:
<asp:Panel ID="passwordPanel" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
<ContentTemplate>
Geben Sie das Passwort ein:<br />
<br />
<asp:TextBox ID="txtPassword" AutoPostBack="false" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnConfirmPassword" runat="server" AutoPostBack="true" Text="Senden" CssClass="button" OnClick="btnConfirmPassword_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="panelUploadDownload" runat="server">
<h2>Upload Paketformeln CSV</h2>
<input type="file" id="myFile" name="myFile" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" CssClass="button" Text="Upload" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<br />
<asp:Label ID="lblStatus" runat="server" Text="statusLabel"></asp:Label>
<br />
<asp:Panel ID="panelChanges" runat="server" CssClass="pnlCSS">
<asp:Label ID="lblChangesHeader" runat="server" Font-Bold="True" ForeColor="Black" Text="Änderungen"></asp:Label>
<br />
<asp:Label ID="lblChanges" runat="server" ForeColor="#009900" Text="changes"></asp:Label>
<br />
<br />
<asp:Button ID="btnConfirm" runat="server" OnClick="btnConfirm_Click" CSSClass="button" Text="Änderungen bestätigen" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<br />
<br />
<h2>Download Paketformeln CSV</h2>
<p><asp:Button ID="btnDownloadCsv" runat="server" OnClick="btnDownloadCsv_Click" Text="Download CSV" CSSClass="button"/></p>
</asp:Panel>
And the C# code:
protected void btnConfirmPassword_Click(object sender, EventArgs e)
{
if (txtPassword.Text == "XX")
{
uploadDownloadPanel.Visible = true;
passwordPanel.Visible = false;
}
}
Load Event
protected void Page_Load(object sender, EventArgs e)
{
mainController = new MainController();
setStatus("", Color.Black);
lblChanges.Visible = false;
lblChangesHeader.Visible = false;
btnConfirm.Visible = false;
panelChanges.Visible = false;
panelUploadDownload.Visible = false;
}
For some reason it doesnt work. Any clues? Triggers?
The SecureString class doesn't allow you to see the value; that's the whole point of it. If you want to be able to work with the value entered into the PasswordBox, use the Password member of PasswordBox instead of the SecurePassword member:
protected void btnConfirmPassword_Click(object sender, EventArgs e)
{
if (txtPassword.Password == "XX")
{
uploadDownloadPanel.Visible = true;
passwordPanel.Visible = false;
}
}
i just removed the updatepanel from the passwordPanel and it worked.
In your load event, do the following
if(IsPostback)
{
uploadDownloadPanel.Visible = false;
}
Actually, every time when you press button, you load event also triggered
You can set your UpdateMode to Always of update panel within panelUploadDownload panel.It will work.
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
I have been have problems with a postback from the button submit as after the button has finished the site think its a full refresh which gives me no chance to show any failure of login message or error,
I cant seem to pinpoint why this is happening and have looked up about the page cycle but can't put my finger on it.
Heres all my code for behind the page
protected void Page_Init(object sender, EventArgs e)
{
Session["user"] = "";
Session["domain"] = "";
Session["manager"] = "";
}
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
lblError.Text = "This was a post back.";
else
lblError.Text = "This was NOT a post back.";
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//Allows the variable to be used through out the app
Session["user"] = username;
Session["domain"] = domain;
Session["manager"] = null;
if (true == authenticateUser(Session["domain"].ToString(), Session["user"].ToString(), password))
Response.Redirect(Response.ApplyAppPathModifier("Update.aspx"));
}
and front
<form id="form1" runat="server" style="Width:19%;" class="pure-form pure- form-stacked">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<br />
<asp:TextBox ID="txtLoginID" Width="95%" Style="display:inline-block;" runat="server"></asp:TextBox>
</div>
<br/>
<asp:Label ID="lblpwd" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" Width="95%" TextMode="Password" runat="server"></asp:TextBox>
<asp:Label ID="lbldmn" runat="server" Text="Domain"></asp:Label>
<asp:DropDownList ID="ddlDomain" Width="95%" runat="server">
<asp:ListItem>hdnl.it</asp:ListItem>
<asp:ListItem>yodel.net</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblError" runat="server" ForeColor="Red" ></asp:Label>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnLogin" runat="server" Text="Login" Width="95%" OnClick="btnLogin_Click"
CssClass="button-success pure-button" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="overlay"></div>
<div class="modal">
<h2>Please Wait.....</h2>
<img alt="Loading..." src="/Images/loading.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</form>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager<asp:UpdatePanel ID="UpdatePanel1"runat="server">
<ContentTemplate>
<asp:Timer runat="server" ID="Timer2" Interval="60" ontick="Timer1_Tick"/>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<b>Test Name:</b> <%# DataBinder.Eval(Container.DataItem, "Name")%> <br />
<b>Test Phone:</b> <%# DataBinder.Eval(Container.DataItem, "Phone")%> <br />
<asp:LinkButton ID="btnView" runat="server" Text="View" CommandName="ShowDetails" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Name")%>' OnCommand="btnView_Command"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Panel ID="panel2" runat="server" Visible="false"></asp:Panel>
Code Behind :
protected void dlBundleRequests_ItemCommand(object source, DataListCommandEventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
if (e.CommandName == "ShowDetails")// null)
{
Session["Name"] = e.CommandArgument.ToString();
//Show Panel2
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//Binding DataList1
}
protected void btnView_Command(Object sender, CommandEventArgs e)
{
//set visibility true for Panel2
}
The necessity of adding updatepanel & timer control is to autorefresh the datalist1 for every 5 minutes.Please help me out of this.After adding these two controls ,link button stops working.
Try this..
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnView"/>
</Triggers>
you must set CommandArgument and CommandName in linkbutton tag, then use Command event not Click
<ItemTemplate>
<b>Test Name:</b> <%# DataBinder.Eval(Container.DataItem, "Name")%> <br />
<b>Test Phone:</b> <%# DataBinder.Eval(Container.DataItem, "Phone")%> <br />
<asp:LinkButton ID="btnView" runat="server" Text="View" OnCommand="btnView_Click" CommandName="ShowDetails" CommandArgument='%# DataBinder.Eval(Container.DataItem, "Phone")%' ></asp:LinkButton>
</ItemTemplate>
then handle that argument in codebehind..
CommandArgument in MSDN
I wanted to update data inside update panel without postback.
I made following code on aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="30">
</asp:ScriptManager>
<asp:UpdatePanel ID="upPanel" runat="server">
<ContentTemplate>
<asp:Label ID="lblCount" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
for updating the label on every half a minute, i written following code on pageload:
protected void Page_Load(object sender, EventArgs e)
{
lblCount.Text = DateTime.Now.ToShortTimeString();
}
But its not updating the label even though i given
AsyncPostBackTimeout="30"
in script manager.
Is anything i am mistaking??
I want to update label inside the updatepanel without postback on certain time interval.
Edit:
<asp:UpdatePanel ID="upPanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblCount" runat="server"></asp:Label>
</ContentTemplate>.
</asp:UpdatePanel>
To update your your page every 30 seconds you can use a timer:
<head runat="server">
<title></title>
<script runat="server" type="text/c#">
protected void Timer1_Tick(object sender, EventArgs e)
{
lblCount.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
}
</script>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="30">
</asp:ScriptManager>
<asp:Timer runat="server" id="Timer1" Interval="30000" OnTick="Timer1_Tick"></asp:Timer>
<asp:UpdatePanel ID="upPanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblCount" runat="server" Text="Page not refreshed yet."></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
As #Nipun Ambastha suggested add the AsyncPostBackTrigger trigger.
Without AsyncPostBackTrigger, the timer has to be placed inside the UpdatePanel:
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Panel refreshed at: " +
DateTime.Now.ToLongTimeString();
}
<form id="form2" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager2">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" ID="Timer2" Interval="30000" OnTick="Timer1_Tick"></asp:Timer>
<asp:Label runat="server" Text="Page not refreshed yet." ID="Label1">
</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label runat="server" Text="Label" ID="Label3"></asp:Label>
</form>
You are actually not using Async Trigger, for updating a panel you will need to declare Async Trigger.
Please check this url
http://msdn.microsoft.com/en-us/library/system.web.ui.asyncpostbacktrigger(v=vs.110).aspx
More Detailed
http://www.asp.net/ajax/documentation/live/overview/UpdatePanelOverview.aspx