使用javascript获取母版页中嵌套面板控件的clientID

发布于 2024-09-29 18:43:25 字数 1474 浏览 4 评论 0原文

这是ContentPlaceHolder的控制结构


  -->向导
      -->面板

我正在使用 setTimeout 在 x 分钟后显示面板。

如何获取面板的ClientID?

所需的 javascript 行类似于:

setTimeout(displayExtendSession('<%= ExtendSession.ClientID  %>', 600000);

aspx

<asp:Content ID="Content1" runat="server" ...>
    <asp:Wizard ID="wizard1" runat="server" ... >
        <asp:Panel ID="ExtendSession" runat="server">
            <asp:Label ID="ExtendSessionLifePrompt" runat="server" Text="Your session is going to expire in 1 minute. Would you like to extend your Session?"></asp:Label>
            <asp:Button ID="ExtendSessionLife" runat="server" Text="Yes" />
            <input type="button" id="CancelExtendSessionLife" value="No" onclick="HideExtendSession('<%= ExtendSession.ClientID %>'); return false;" />  
        </asp:Panel>
    </asp:Wizard>
</asp:Content>

javascript

function HideExtendSession(msgBox) {
    if (msgBox)
        document.getElementById(msgBox).style.display = "none";
}
function DisplayExtendSession(msgBox) {
    if (msgBox) 
        document.getElementById(msgBox).style.display = "block";
}

setTimeout(DisplayExtendSession('<%= ExtendSession.ClientID  %>', 600000);
setTimeout(HideExtendSession('<%= ExtendSession.ClientID %>', 720000);

This is the control structure

ContentPlaceHolder
  --> Wizard
      --> Panel

I am using setTimeout to display the panel after x minutes.

How do I get the ClientID of the panel?

The line of javascript needed is something like:

setTimeout(displayExtendSession('<%= ExtendSession.ClientID  %>', 600000);

aspx

<asp:Content ID="Content1" runat="server" ...>
    <asp:Wizard ID="wizard1" runat="server" ... >
        <asp:Panel ID="ExtendSession" runat="server">
            <asp:Label ID="ExtendSessionLifePrompt" runat="server" Text="Your session is going to expire in 1 minute. Would you like to extend your Session?"></asp:Label>
            <asp:Button ID="ExtendSessionLife" runat="server" Text="Yes" />
            <input type="button" id="CancelExtendSessionLife" value="No" onclick="HideExtendSession('<%= ExtendSession.ClientID %>'); return false;" />  
        </asp:Panel>
    </asp:Wizard>
</asp:Content>

javascript

function HideExtendSession(msgBox) {
    if (msgBox)
        document.getElementById(msgBox).style.display = "none";
}
function DisplayExtendSession(msgBox) {
    if (msgBox) 
        document.getElementById(msgBox).style.display = "block";
}

setTimeout(DisplayExtendSession('<%= ExtendSession.ClientID  %>', 600000);
setTimeout(HideExtendSession('<%= ExtendSession.ClientID %>', 720000);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2024-10-06 18:43:25

要获取 ExtendSession 的客户端 ID,我认为您需要做的是:

setTimeout(displayExtendSession('<%= wizard1.FindControl("ExtendSession").ClientID  %>', 600000);

更新

如果您无法访问 Wizard1,那么也许是这样:

setTimeout(displayExtendSession('<%= Content1.FindControl("wizard1").FindControl("ExtendSession").ClientID  %>', 600000);

我希望使用双引号没关系,但我相信如果这没有帮助,你可以改变它们。

To get the client Id of ExtendSession I think what you need to do is:

setTimeout(displayExtendSession('<%= wizard1.FindControl("ExtendSession").ClientID  %>', 600000);

updated

if you can't get to wizard1, then maybe this:

setTimeout(displayExtendSession('<%= Content1.FindControl("wizard1").FindControl("ExtendSession").ClientID  %>', 600000);

I would expect the double quotes to be fine, but I'm sure you can switch them around if that's not helping.

妖妓 2024-10-06 18:43:25

如果您添加

ClientIDMode="Static"

<asp:Panel ID="ExtendSession" runat="server">

asp.net中

<asp:Panel ID="ExtendSession" ClientIDMode="Static" runat="server">

,则不会破坏您的面板的 id,并且

document.getelementbyid("ExtendSession")

会返回

的 id;由 asp:panel 控件呈现。

If you add

ClientIDMode="Static"

to

<asp:Panel ID="ExtendSession" runat="server">

resulting in

<asp:Panel ID="ExtendSession" ClientIDMode="Static" runat="server">

asp.net will not mangle your panel's id and

document.getelementbyid("ExtendSession")

will return the id of the <div> rendered by the asp:panel control.

深海夜未眠 2024-10-06 18:43:25

如果您不太关心性能等,

以下代码可能是轻松完成任务的一种方法。

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(FindRecursiveControl(this,"ExtendSessionLife").ClientID.ToString());
}

Control FindRecursiveControl(Control cd, string Name)
{
    if (cd.ID == Name && cd.ID !=null)
        return cd;

    foreach (Control c in cd.Controls)
    {
        Control cfind = FindRecursiveControl(c, Name);

        if (cfind != null)
            return cfind;
    }

    return null;
}

If you are not much into performance and all,

Following code might be one way to easily achieve the tasks.

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(FindRecursiveControl(this,"ExtendSessionLife").ClientID.ToString());
}

Control FindRecursiveControl(Control cd, string Name)
{
    if (cd.ID == Name && cd.ID !=null)
        return cd;

    foreach (Control c in cd.Controls)
    {
        Control cfind = FindRecursiveControl(c, Name);

        if (cfind != null)
            return cfind;
    }

    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文