0

ページにこの asp:label があり、必要なのはテキストを時々変更することです。しかし、コードを実行すると、ページごとにラベル ID が変わります。「ctl00_bh_」何かが追加されています...

これを修正するには?

ここに私のコードスニペットがあります。

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.");
    }
}

ありがとう

4

1 に答える 1

1

サーバー側のコントロールにアクセスしようとしている Javascript では、ClientID を使用する必要があります。

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

または、.Net Framework 4 以降を使用している場合は、aspx ページのClientIDMode

<asp:Label ID="lblSessionTime" runat="server" ClientIDMode="Static" />
于 2012-07-06T05:34:32.553 に答える