Link to home
Start Free TrialLog in
Avatar of rwallacej
rwallacej

asked on

ASP.net / HTML checkbox list and listbox - make items same height

Hi
I have table with three columns, see code.
Column 1 has a Listbox, Columns 2 & 3 have CheckboxList
I want the rows to be same height so they are lined up. As more items are added to the CheckboxList they get increasingly higher and out of line with the listbox.
How can this be done?
Thanks in advance for code

<table>
            <tr>
                <td>
                    A
                </td>
                <td>
                    B
                </td>
                <td>
                    C
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ListBox ID="ListBox2" runat="server" Height="95px">
                        <asp:ListItem>Field 1</asp:ListItem>
                        <asp:ListItem>Field 2</asp:ListItem>
                        <asp:ListItem>Field 3</asp:ListItem>
                        <asp:ListItem>Field 4</asp:ListItem>
                    </asp:ListBox>
                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList3" runat="server">
                        <asp:ListItem>Field 1</asp:ListItem>
                        <asp:ListItem>Field 2</asp:ListItem>
                        <asp:ListItem>Field 3</asp:ListItem>
                        <asp:ListItem>Field 4</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList4" runat="server">
                        <asp:ListItem>Field 1</asp:ListItem>
                        <asp:ListItem>Field 2</asp:ListItem>
                        <asp:ListItem>Field 3</asp:ListItem>
                        <asp:ListItem>Field 4</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
        </table>

Open in new window

Avatar of mvgeertruyen
mvgeertruyen
Flag of Belgium image

Depending on your application consider changing the Listbox to another control... (Like radio buttons if you just need  a single selections. I assume you want the options in each column to align)

Hope it helps
    <table>
            <tr>
                <td>
                    A
                </td>
                <td>
                    B
                </td>
                <td>
                    C
                </td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem>Field 1</asp:ListItem>
                        <asp:ListItem>Field 2</asp:ListItem>
                        <asp:ListItem>Field 3</asp:ListItem>
                        <asp:ListItem>Field 4</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList3" runat="server">
                        <asp:ListItem>Field 1</asp:ListItem>
                        <asp:ListItem>Field 2</asp:ListItem>
                        <asp:ListItem>Field 3</asp:ListItem>
                        <asp:ListItem>Field 4</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList4" runat="server">
                        <asp:ListItem>Field 1</asp:ListItem>
                        <asp:ListItem>Field 2</asp:ListItem>
                        <asp:ListItem>Field 3</asp:ListItem>
                        <asp:ListItem>Field 4</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
        </table>

Open in new window

Hi,

You can put the panel with fixed (same height) and put listbox or checkbox list on it.
Then set the scrollbar of the panel to vertical.


Regards
Bedanand
http://www.dot4pro.com

ASKER CERTIFIED SOLUTION
Avatar of GuitarRich
GuitarRich
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwallacej
rwallacej

ASKER

hi
Thanks for contributions.

The items in Column 1 (titled 'A') are added dynamically...then they are added to the two checkbox lists too titled 'B' and 'C'.

Users check Column 'B' or Column 'C' for every row

I like the idea of Repeater, it keeps everything lined up nicely. How does one add items dynamically to it? I also need to read every row to know what user has selected on every row.

Thanks again
So in your example the radio buttons are added dynamically?  How are they added - via a database call?  To add items to a repeater you need to bind the repeater to a datasource.
So you could populate your datasource, like a datatable with all the details required to create the radio button. Then add them in the repeater as in my example.
To access the checkboxes in code behind you can look at the Repeaters item collection. Example below

		<asp:Repeater ID="Repeater1" runat="server">
			<ItemTemplate>
				<tr>
					<td><asp:RadioButton ID="radioButton1" runat="server" Text='<%# Eval("FieldName") %> /></td>
					<td><asp:CheckBox ID="checkBox1" runat="server" /></td>
					<td><asp:CheckBox ID="checkBox2" runat="server" /></td>
				</tr>
			</ItemTemplate>
		</asp:Repeater>
 
 
///////////////////////////////////////////////////////////
 
 
		foreach (RepeaterItem itm in repeater1.Items)
		{
			CheckBox chk1 = (CheckBox)itm.FindControl("checkBox1");
			if (chk1.Checked)
			{
				// Do something
			}
			// etc... for checkBox2
		}

Open in new window

Thanks, yes they are to be added dynamically.
There are a list of items in a combobox and the user selects and item, then clicks "Add" and the selected item should create a new row. All that is in the list is an array of strings, it isn't bound to a database
so the Repeater needs to be bound to a string array - each entry in array is a new repeater row, which is the bit I am missing now
Just bind the repeater to the array like this:

repeater1.DataSource = stringArray;
repeater1.DataBind();
 
		<asp:Repeater ID="Repeater1" runat="server">
			<ItemTemplate>
				<tr>
					<td><asp:RadioButton ID="radioButton1" runat="server" Text='<%# Containter.DataItem %> /></td>
					<td><asp:CheckBox ID="checkBox1" runat="server" /></td>
					<td><asp:CheckBox ID="checkBox2" runat="server" /></td>
				</tr>
			</ItemTemplate>
		</asp:Repeater>

Open in new window

Thank-you very much your help is very much appreciated!
Regards,
rwallacej