mas_oz2k1
mas_oz2k1

Reputation: 2901

how to Resize child list box height in asp.net User control

Given the following User control markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SearchItem.ascx.cs" Inherits="MyWeb.controls.SearchItem" %>

<div id="container" runat="server" style="height: 100%; width: 100%;">
    <div>
        <asp:Label ID="lblSearch" runat="server" Text="Search:"></asp:Label>
        <br />
        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        <input id="btnSearch" type="button" value="Search" onclick="SearchClick(this)" />
</div>
<div>
    <asp:Label ID="lblItems" runat="server" Text="Available Items:"></asp:Label>
</div>
<div id="itemContents" runat="server" style="min-height: 50%; width: 100%;border: 1px solid black;">
    <asp:ListBox ID="lbxResults" runat="server" SelectionMode="Single" Width="100%" AutoPostBack="True"></asp:ListBox>
</div>

I would like the user control height to be equal to placeholder height with list box to grow and fill up any height difference as all other controls are of known size. Some useful info:

For testing purposes, I did:

  1. Create a new user control, eg. SearchItem.ascx

  2. Create a default aspx page for example:

a) Add the user control //<%@ Register TagPrefix="uc" TagName="SearchItem" Src="~/Controls/SearchItem.ascx" %>

b) Add to body

    <form id="form1" runat="server">
<div id="ChangeMyHeight" style="height: 300px; width: 30%; float: left;">
    <uc:SearchItem ID="AvailableItems" runat="server">
    </uc:SearchItem>
</div>
</form>

Now if you change the height of the container div to 500, 600.(ChangeMyHeight), you will have:

Current behaviour:

Expected behaviour:

My preference would be a css/jquery solution but code behind will do as well (I was thinking of using a height property to set up the child controls)

Notes:

  1. The sample markup uses style for brevity's sake.
  2. The sample markup has hard coded some values for demo purposes(thus the demo is working for height 300 px but not for the other values), feel free to change style/html as needed.
  3. Environment: VS2010/.NET 4.0/jQuery latest.
  4. Browsers: FF, IE7/8/9

Upvotes: 2

Views: 2599

Answers (3)

mas_oz2k1
mas_oz2k1

Reputation: 2901

The answer requires some Jquery code to recalculate the internal list box height based on the current parent's height. Add this code to the head section of the test page:

<script type='text/javascript'>
//Method to resize when first loaded or resized, added to admin page.
$(function () {
    ResizeHeightControlBasedOnParent('<%= ucSearchUser.ItemContentsId %>');
});

function ResizeHeightControlBasedOnParent(targetId) {
    var obj = $('#' + targetId);
    if (obj.length > 0 && obj.height() > 0) {
        var objParent = obj.parent();
        obj.height(function (index, height) {
            var objParent = $(this).parent();
            if (objParent.height() <= 0)
                return 0;
            return objParent.height() + objParent.offset().top - $(obj[0]).offset().top;
        });
    }
}
//Helper to test it works for diff div heights.Added to helper onclick button
function changeHeight() {
    var newH = 300 + Math.ceil(Math.random() * 300);
    document.getElementById('changeMyHeight').style.height = newH + 'px';
    ResizeHeightControlBasedOnParent('<%= ucSearchUser.ItemContentsId %>');
}

</script>

where ItemContentsId is defined in the user control code behind as follows:

public string ItemContentsId
{
    get { return this.itemContents.ClientID; }//Listbox id
}

Upvotes: 0

use css select[attribute] { height:400px;}

in this attribute = unique attribute of your child listbox id

Upvotes: 0

---------Default

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PlayGround.Default" %>

<script type="text/javascript">


    function changeHeight() {

        document.getElementById('ChangeMyHeight').style.height = Math.ceil(Math.random() * 1000) + 'px';
    }
</script>

<form id="form1" runat="server">


    <input id="Button1" type="button" value="Change Height" onclick="changeHeight()" />

<div id="ChangeMyHeight" style="height: 300px; width: 30%; float: left;border:1px solid red;">

    <uc1:SearchItem runat="server" ID="SearchItem" />

</div>
</form>

------Control

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SearchItem.ascx.cs" Inherits="PlayGround.SearchItem" %>
<asp:ListBox ID="ListBox1" ClientIDMode="Static" runat="server" style="height:100%"></asp:ListBox>

------Control Behind

 protected void Page_Load(object sender, EventArgs e)
    {
        var cnt = 1999;

        for (int i = 0; i < cnt; i++)
        {
            this.ListBox1.Items.Add(Path.GetRandomFileName());
        }
    }

Upvotes: 1

Related Questions