2012-07-06 30 views
0

我在我的页面中有这个asp:标签,我想要的是不时更改文本。但是,当我运行代码时,标签ID在不断翻页。它被附加了“ctl00_bh_”的东西...标签ID正在改变,如何解决这个问题?

如何解决这个问题?

这里是我的代码片段。

protected void Page_Load(object sender, EventArgs e) 
{ 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad", "DisplaySessionTimeout()", true); 
} 

<asp:Label ID="lblSessionTime" runat="server" /> 

function DisplaySessionTimeout() { 
    document.getElementById("lblSessionTime").innerHTML = "updating text here"; 
    sessionTimeout = sessionTimeout - 1; 
    if (sessionTimeout >= 0) 
     window.setTimeout("DisplaySessionTimeout()", 1000); 
    else 
    { 
     alert("Your current Session is over."); 
    } 
} 

感谢

回答

1

在你的JavaScript您试图访问服务器端控件,您需要使用客户端ID,尝试

document.getElementById("<%= lblSessionTime.ClientID%>").innerHTML ="updating text here"; 

或者ClientIDMode在aspx页面,如果你正在使用。网络框架4或更高版本

<asp:Label ID="lblSessionTime" runat="server" ClientIDMode="Static" /> 
+0

第二个作品...谢谢 – user1120260 2012-07-06 05:46:08

+1

@ user1120260,y欢迎您,如果合适,您可以接受答案。请参阅[接受答案的工作方式?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Habib 2012-07-06 05:48:18