s15199d
s15199d

Reputation: 7717

AJAXToolkit Dynamically Hide a tab?

I know how to hide a tab:

MyTabContainerID.Tabs[1].Visible = false;

That works. What I'm having trouble with is changing the visibility of a tab triggered by the postback of a radioButtonList selectedIndexChanged event.

By the time the page reaches my selectedIndexChanged event handler the tab has already loaded Visible=True from the ViewState. I can change it to false all day long in my selectedIndexChanged event, it won't hide the tab b/c it's already loaded.

ASPX

<asp:RadioButtonList ID="rblMyRadioButtonList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblMyRadioButtonList_SelectedIndexChanged">                                                        
<asp:ListItem Text="Yes" Value="True" Selected="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>

ASCX

protected void Page_Init(object sender, EventArgs e)
{
    try
    {
        MyTabContainerID.Tabs[1].Visible = Tab1Visibility;
    }
    catch (Exception ex)
    {
        common.alert("Error in PageName.Page_Init.<br>ERROR=" + ex.Message);
    }
}
protected void rblMyRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {       
        Tab1Visibility = Convert.ToBoolean(((RadioButtonList)sender).SelectedValue);
        MyTabContainerID.Tabs[1].Visible = Tab1Visibility;  //BY THE TIME THIS HAPPENS IT'S ALREADY ON THE PAGE WITH VISIBLE=TRUE                   
    }
    catch (Exception ex)
    {
        common.alert("Error in PageName.rblMyRadioButtonList_SelectedIndexChanged.<br>ERROR=" + ex.Message);
    }
}

protected bool Tab1Visibility
{
    get { return (bool)Session["ses_bTab1Visibility"]; }
    set { Session["ses_bTab1Visibility"] = value; }
}

Thoughts? Suggestions?

Upvotes: 2

Views: 1420

Answers (2)

s15199d
s15199d

Reputation: 7717

Here's what I ended up doing:

*.ASPX

<script type="text/javascript">
    function disableTab1() {
            $find('<%=MyTabContainer.ClientID%>').get_tabs()[1].set_enabled(false);
        }
    function enableTab1() {
        $find('<%=MyTabContainer.ClientID%>').get_tabs()[1].set_enabled(true);
    }
</script>

<!--THESE RADIOBUTTONS LEVERAGE THE TAB CONTAINER'S CLIENTSIDE set_enabled METHOD
<asp:RadioButtonList ID="rblMyRadioButtonList" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem Text="Yes" Value="True" Selected="True" onClick="enableTab1();"></asp:ListItem>
    <asp:ListItem Text="No" Value="False" onClick="disableTab1();"></asp:ListItem>
</asp:RadioButtonList>

*.ASCX on Page_Load

//I RUN A SQL QUERY TO DETERMINE WHETHER OR NOT TO DISABLE TAB1 HERE 
//IF IT SHOULD BE DISABLED I REGISTER THIS STARTUP SCRIPT
ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();
String csScriptName = "Tab1_Visibility";
if (!cs.IsStartupScriptRegistered(cstype, csScriptName))
{
    cs.RegisterStartupScript(this.Page.GetType(), csScriptName, "setTimeout(function() { disableTab1()}, 0);", true);
}

Upvotes: 0

Crab Bucket
Crab Bucket

Reputation: 6277

You should be able to set the Visibility of your tab control in the rblMyRadioButtonList_SelectedIndexChanged method. Even though the tab control's state will have been loaded from the ViewState at this point it can still be overridden.

I had a similair problem where Visibilty just couldn't be set. It was driving me mad. The solution was that the visiblility was being explicitly set in a container (parent) control. This will override the setting for the child control. Could that be your issue.

Other than that then do the usual. Breakpoint on rblMyRadioButtonList_SelectedIndexChanged and watch values. Also - can you manually force the Visibility on Page_PreRender - which is further downstream still.

EDIT

The other thing that could affect it is update panels. If the radiobutton list is in an update panel and the tab is outside of it then the update wouldn't work as the part of the page with the tab wouldn't be refreshed.

Upvotes: 2

Related Questions