By using
Timer control , you can auto refresh a user control inside a page in asp.net.
You can use timer control along with update panel control to auto refresh a
user control.
Let’s see a
simple example . I have created a user control and I keep one label, one timer
and update panel control. Put label control inside update panel control. Keep
timer control outside of update panel control.
UserControl.ascx:-
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplicationTest.UserControl1" %>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
<asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
</Triggers>
<ContentTemplate>
Time On UserControl :
<asp:Label runat="server" id="DateLabel" />
</ContentTemplate>
</asp:UpdatePanel>
UserControl.ascx.cs:-
protected
void UpdateTimer_Tick(object sender, EventArgs e)
{
DateLabel.Text
= DateTime.Now.ToString();
}
Now create one default.aspx page and drag and drop
usercontrol.ascx control on default.aspx page.
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationTest.Default" %>
<%@ Register src="UserControl1.ascx" tagname="UserControl1" tagprefix="uc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Time
on Default Page:<asp:Label runat="server" id="Label1" />
</div>
<uc1:WebUserControl1 ID="UserControl11" runat="server" />
</form>
</body>
</html>
Default.aspx.cs:-
protected
void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
This is very simple. In Above code Timer is used, which
will Tick every 5 second. So every 5 seconds it will show the updated time on
user control.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.