Timer control's Tick event is not working on WSS Web Part
Sample Code is :-
Code from .cs file
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
StrConnection =
"data source=" + System.Configuration.ConfigurationManager.AppSettings["Server"].ToString() + ";initial catalog=" + System.Configuration.ConfigurationManager.AppSettings["Database"].ToString() + ";User ID=" + System.Configuration.ConfigurationManager.AppSettings["Username"].ToString() + ";Password=" + System.Configuration.ConfigurationManager.AppSettings["Password1"].ToString() + ";persist security info=True;packet size=4096";
getSelectData();
Label1.Text =
DateTime.Now.ToLongTimeString();
timer =
new Timer();
timer.Interval = 1000;
timer.Tick +=
new EventHandler(TimerHandler);
}
catch (System.Exception ErrMessage)
{
Response.Write(ErrMessage.Message.ToString());
}
}
}
Code from Html
<
asp:ScriptManager ID="ScriptManager" runat="server"/>
<
asp:UpdatePanel ID ="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
Alle Antworten
- i meet same ,i want to show system-time in moss webparts,and refurbisn 1s,but when
refreshName.ContentTemplateContainer.Controls.Add(tm);
or
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger(); all error
using System; using System.Text; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; namespace HelloWebPart { [Guid("35bce816-acb9-4251-bf36-7981503535e9")] public class HelloWebPart : Microsoft.SharePoint.WebPartPages.WebPart { public HelloWebPart() { this.ExportMode = WebPartExportMode.All; } private Label displayName; private TextBox inputName; private Label showTime; string h = DateTime.Now.Hour.ToString();//获取系统时间小时 string m = DateTime.Now.Minute.ToString();//获取系统时间分钟 string s = DateTime.Now.Second.ToString();//获取系统时间秒 UpdatePanel refreshName = new UpdatePanel(); Timer tm = new Timer(); protected override void OnInit(EventArgs e) { base.OnInit(e); } protected override void CreateChildControls() { base.CreateChildControls(); //Fix for the UpdatePanel postback behaviour. EnsurePanelFix(); LinkButton sayHello = new LinkButton(); ScriptManager scriptHandler = new ScriptManager(); displayName = new Label(); inputName = new TextBox(); showTime = new Label(); this.showTime.ID = "showtime"; this.showTime.Text = "T:" + h + "时" + m + "分" + s + "秒"; //Set up control properties. this.displayName.ID = "displayName"; this.displayName.Text = "Hello!"; this.inputName.ID = "inputName"; sayHello.ID = "sayHello"; sayHello.Text = "Say Hello"; scriptHandler.ID = "scriptHandler"; refreshName.ID = "refreshName"; //refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional; refreshName.UpdateMode = UpdatePanelUpdateMode.Always; refreshName.ChildrenAsTriggers = true; //Add the EventHandler to the Button. sayHello.Click += new EventHandler(ClickHandler); tm.Enabled = true; tm.ID = "tm"; tm.Interval = 1000000; tm.Tick += new EventHandler<EventArgs>(Tm_Tick); //refreshName.ContentTemplateContainer.Controls.Add(tm); AsyncPostBackTrigger trigger = new AsyncPostBackTrigger(); trigger.ControlID = "tm"; refreshName.Triggers.Add(trigger); //Add the user interface (UI) controls to the UpdatePanel. refreshName.ContentTemplateContainer.Controls.Add(this.inputName); refreshName.ContentTemplateContainer.Controls.Add(sayHello); refreshName.ContentTemplateContainer.Controls.Add(this.displayName); //refreshName.ContentTemplateContainer.Controls.Add(tm); refreshName.ContentTemplateContainer.Controls.Add(this.showTime); //The ScriptManager control must be added first. this.Controls.Add(scriptHandler); this.Controls.Add(refreshName); //this.Controls.Add(tm); } private void ClickHandler(object sender, EventArgs args) { this.displayName.Text = "Hello, " + this.inputName.Text.ToString() + "."; } protected void Tm_Tick(object sender, EventArgs e) { this.showTime.Text = DateTime.Now.ToString(); } protected override void RenderWebPart(HtmlTextWriter output) { RenderChildren(output); } //修改脚本以确保正确的 doPostBack() 行为 private void EnsurePanelFix() { if (this.Page.Form != null) { String fixupScript = @" _spBodyOnLoadFunctionNames.push(""_initFormActionAjax""); function _initFormActionAjax() { if (_spEscapedFormAction == document.forms[0].action) { document.forms[0]._initialAction = document.forms[0].action; } } var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction; RestoreToOriginalFormAction = function() { if (_spOriginalFormAction != null) { RestoreToOriginalFormActionCore(); document.forms[0]._initialAction = document.forms[0].action; } }"; ScriptManager.RegisterStartupScript(this, typeof(HelloWebPart), "UpdatePanelFixup", fixupScript, true); } } } }

