?
Solved

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

Posted on 2009-04-29
8
Medium Priority
?
1,076 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:rwallacej
8 Comments
 
LVL 6

Expert Comment

by:mvgeertruyen
ID: 24258184
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

0
 
LVL 8

Expert Comment

by:bedanand
ID: 24258212
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

0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 2000 total points
ID: 24258311
Could you use a datagrid or repeater instead of the checkbox/radiobuttonlist? Example of repeater below. This would keep all the controls in-line with each row and you can reference them in the code behind without too much trouble.
	<table>
        <tr>
            <td>
                A
            </td>
            <td>
                B
            </td>
            <td>
                C
            </td>
        </tr>
		<asp:Repeater ID="Repeater1" runat="server">
			<ItemTemplate>
				<tr>
					<td><asp:RadioButton ID="radioButton1" runat="server" /></td>
					<td><asp:CheckBox ID="checkBox1" runat="server" /></td>
					<td><asp:CheckBox ID="checkBox2" runat="server" /></td>
				</tr>
			</ItemTemplate>
		</asp:Repeater>
	</table>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rwallacej
ID: 24258671
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
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24258802
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

0
 

Author Comment

by:rwallacej
ID: 24259086
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
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24259117
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

0
 

Author Closing Comment

by:rwallacej
ID: 31575818
Thank-you very much your help is very much appreciated!
Regards,
rwallacej
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question