How can I set child tab height in a nested ajax asp.net tab?
Asked Answered
D

1

7

I have 2 Nested ajax asp.net tabs. If I set the initial height of the parent tab (TabContainerMain) to say 300, how can I set the height of child tab (SubTabContainerUg) in css or jquery? The markup is shown below:

<!DOCTYPE html>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<html lang="en">
<head runat="server">
    <title></title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <script src="../js/jquery-1.6.2.js" type="text/javascript"></script>
    <script src="../js/modernizr-latest.js" type="text/javascript"></script>
    <script src="../js/admin.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </cc1:ToolkitScriptManager>
    <section>
        <cc1:TabContainer ID="TabContainerMain" runat="server" Height="300px">
            <cc1:TabPanel ID="tp2" runat="server" HeaderText="test 2">
                <ContentTemplate>
                <section>
                    <div style="height: 100%; width: 30%; float: left;">
                        <div>
                            <asp:Label ID="Label5" runat="server" Text="Search:"></asp:Label>
                            <br />
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            <input id="Button1" type="button" value="Search" onclick="SearchClick(this)" />
                        </div>
                    </div>
                    <div style="width: 70%; float: left;">
                        <cc1:TabContainer ID="SubTabContainerUg" runat="server" ActiveTabIndex="0">
                            <cc1:TabPanel ID="subTab1" runat="server" HeaderText="Cubes">
                                <ContentTemplate>
                                <div style="height: 100%;">
                                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                </div>
                                </ContentTemplate>
                            </cc1:TabPanel>
                            <cc1:TabPanel ID="subTab2" runat="server" HeaderText="Reports">
                                <ContentTemplate>
                                    <div>
                                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                    </div>
                                </ContentTemplate>
                            </cc1:TabPanel>
                        </cc1:TabContainer>
                    </div>
                </section>
                </ContentTemplate>
            </cc1:TabPanel>
            <cc1:TabPanel ID="tp3" runat="server" HeaderText="test 3">
                <ContentTemplate>
                </ContentTemplate>
            </cc1:TabPanel>
        </cc1:TabContainer>
    </section>
    <footer> This is footer. </footer>
    </form>
</body>
</html>

Note: 1) My initial non-maintanable solution was to set this height in the code behind server as follows:

SubTabContainerUg.Height = new Unit(TabContainerMain.Height.Value - 43);

Notice that this code works correctly for IE7/8/9, that is resizing the sub tab and keeping the container at the same height (= 300 px), but code above is not good because if I change the style(margins/borders) of the child tab container then the above code (to be precise the hardcoded constant) must change too.

2) I simplified the markup for brevity'sake. Both main tab and child tab will contain controls like textboxes and list boxes.

3) This page is used as a dialog and runs in FF and IE7/8/9.

Dorsal answered 2/4, 2013 at 4:30 Comment(0)
S
4

Answer

The solution to your issue is relatively simple to achieve using jQuery. For each TabContainer that you want to resize to its parent's size, you must:

// SubTabContainerUg is the TagContainer's ID property.
var myheader = $('#<%= SubTabContainerUg.ClientID %> > .ajax__tab_header');
var mybody = $('#<%= SubTabContainerUg.ClientID %> > .ajax__tab_body');
var myparentbody = $('#<%= TabContainerMain.ClientID %> > .ajax__tab_body');
mybody.height(myparentbody.height() - myheader.outerHeight(true) - myheader.position().top);

Details

  1. Find the child tab control's header element (which holds the tab buttons);

    var myheader = $('#<%= SubTabContainerUg.ClientID %> > .ajax__tab_header');
    
  2. Find the child tab control's body element (the body of the selected tab);

    var mybody = $('#<%= SubTabContainerUg.ClientID %> > .ajax__tab_body');
    
  3. Find the parent tab control's body element (where the child tab control is located);

    var myparentbody = $('#<%= TabContainerMain.ClientID %> > .ajax__tab_body');
    
  4. Set the child tab control's body height to be the parent tab control's body height (minus the child tab's header height and top position - the top position includes any parent padding, and any element margin).

    mybody.height(myparentbody.height() - myheader.outerHeight(true) - myheader.position().top);
    

This solution worked in:


Edit: Changed offset() to position(). Per the position() docs:

When positioning a new element near another one and within the same containing DOM element, .position() is the more useful.


Edit 2: Here's a Gist of your markup, here's my markup and code, and here's a revision showing the differences.

  1. I reference http://code.jquery.com/jquery-1.6.2.min.js - hopefully you are referencing an original and unmodified copy of it in your code.

  2. Removed height: 100%; from the first div inside cc1:TabPanel ID="tp2".

  3. Changed the second div from float: left; to float: right; (this div contains the cc1:TabContainer ID="SubTabContainerUg").

  4. Removed style="height: 100%;" from the div inside cc1:TabPanel ID="subTab1".

Succeed answered 9/4, 2013 at 19:12 Comment(19)
Good effort. Question: Where did you locate this code? Is it at OnClientActiveTabChange event in the main tab? If so this sample code always execute whether the sub tabs are visible or not.We only need to change height when sub-tab is visible. (e.g if mybody.height() > 0)Dorsal
I had set the code to execute on page load; however, you can place it inside of a function, and call that function from the OnClientActiveTabChange event.Succeed
No, it does not work. I added your code as is to the OnClientActiveTabChange. I notice that both mybody.height() and myparentbody.height() are returning 0 and executing all the time the user switch tabs, thus code need to have extra logic to check if it is the tab containing the sub-tabDorsal
@Dorsal I've updated my answer with Edit 2 - it details all of the changes I made on your original markup.Succeed
Thanks for your patience. Still does not work, it is not finding my header and mybody. Are you using VS2010 or VS2012?. Also if you can post your web.config file will be great. I may be missing something there. Also what version of the control library are you are using?Dorsal
This solution works for IE9 only if I change the selector queries to:var myheader = $('#<%= SubTabContainerUg.ClientID %>_header'); var mybody = $('#<%= SubTabContainerUg.ClientID %>_body'); var myparentbody = $('#<%= TabContainerMain.ClientID %>_body'); BUT IT does not work for IE7/8, the subtab height is a bit bigger (256 (IE9) vs. 271(IE8) vs260 (IE7). Please include your web.config file and version of ajax library just in case. (I am using VS2010)Dorsal
I'm using VS2010, and AjaxControlToolkit 7.0123 from NuGet. And here's my web.config with sensitive details removed - this is a .NET 3.5 project.Succeed
I just tested the site again on IE6/7/8, and it works just fine. Can you place a test page online somewhere? (Not just the code, a page I can access remotely).Succeed
I use .NET 4.0/VS2010 and the same Ajax version for .NET 4.0.Did you check the pixel sizes of both main and sub tab container? This code does not WORK in IE7 because the main tab is 317 px even though the style sets it to 300 px. (I am talking about of checking that sizing works the same way on all browsers avoinding the nasting IE box model oddities - I do not think your test include size check). Please put your page online as you did before, so I can give you screen shots.Dorsal
@Dorsal The page is available here. Does this page not work for you?Succeed
@Jessy: Thanks again but I still have the same error, please use Dev tools to compare style value with layout value in IE7 mode, you will notice the discrepancy between height values in the main tab (style=300 but layout=317) and the sub tab height is 264. Those values must be the same as IE9=>main tab (style=layout=300) and subtab (style=layout=258). Sadly for me is a big deal because this page is used as a dialog, thus any height changes affects the dialog layout else where.Dorsal
@Dorsal Can you place a non-working copy of your code online somewhere? Maybe even post the code on GitHub, Pastebin, or any other code-dump so that I can copy it and troubleshoot it. Without seeing what's going on in your code, I am limited as to how I can help. I've provided full source code, an online copy of the working page, and even screenshots in every major browser (except Opera)!Succeed
Your provided link(that uses your code) does not work, please retest your link but use IE7 and check the heights and you will notice the diference (style tab vs layout tab info). I tested with the same code you published as I stated previously your code only works for IE9 but not IE7.Dorsal
Hm, from your comment above, you state "...please use Dev tools to compare style value with layout value in IE7 mode..." - Are you using the Browser Mode: IE7 in Internet Explorer's Developer Tools?Succeed
@Dorsal Since you haven't answered, I'll just add my comment - using the different "Browser Modes" available from Internet Explorer's Developer Tools is not recommended. I have tested several versions (IE9 and 10), and they are horribly unreliable. The safest bet is using the native browsers themselves.Succeed
Not for this case, I use dev tools that as quick check, but I checked this page in old VM (native IE7) and asame problem. As stated in question: solution must work for IE7 to 9.Dorsal
@Dorsal Ok, then we have different version of Internet Explorer. All of the versions I tried work, yours don't. I've shown screenshots of what mine look like, you haven't. Unless you help me help you, I am done with this answer.Succeed
IE9 and IE10 are more standard versions, the question specifically ask for IE7. Can you post the IE7 version you are using? Usually problems are related when we change major version 7-8. (But I suppose with M$ IE anything can happen). Just to clarify again your code resize the sub tab in IE7 but NOT at the same SIZE as IE9. You need to use pixel ruler or Dev tools to see the difference. (17 px)Dorsal
@Dorsal - ok, I'll see what I can do. I'll try to get back to you soon.Succeed

© 2022 - 2024 — McMap. All rights reserved.