开发者

Get the clientID of nested panel control in masterpage using javascript

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.ge开发者_StackOverflow社区tElementById(msgBox).style.display = "block";
}

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


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.


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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜