Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How set minimum width of listbox?

Posted on 2009-04-30
7
Medium Priority
?
2,304 Views
Last Modified: 2012-06-21
How can I set the MINIMUM width of a listbox control, so that when there are NO ITEMS it is 150px, but when there are items in it the listbox gets wider to FIT the longest item, so there's no scrollbar?
0
Comment
Question by:RobertNZana
  • 3
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 24276866
I don't think that would be a proper design. Imagine if the width of the text is 500px then it will ruin the other controls on the page.  So what I suggest is this.

Use the title tag to show a tooltip to the user.

Use the databound event of the listbox
protected void ListBox1_DataBound(object sender, EventArgs e)
        {
            foreach (ListItem li in ListBox1.Items)
            {
                li.Attributes.Add("title", li.Text);
            }
        }

Open in new window

0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24277223
Hi,

on the Page_Load event you could check if there are any items in the listbox and if not set the width to 150px. Also i you  might be able to loop through the items in the list box and get the length of the logest and work off that. Something like:

Page_Load()
{
   if(ListBox.Items.Count = 0)
   {
       ListBox.Width = 150px;
   }
}

HTH
-M3mph15
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 24277242
I had that part already.  But, let's say it's set to 150px then the user adds an item to the listbox.  I cannot set it to 0, or it's too narrow.  How can I "NULLIFY" the value once it's set?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:M3mph15
ID: 24292385
Hi,

Why do you want to Nullify the value?
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 24292465
If a value for the width is set I cannot "remove" the defined width value.  Setting it to 0 doesn't work either.  I need to put it in a state as if the width was never set, so it will expand dynamically based on the largest listitem width.
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 1500 total points
ID: 24292708
Ah ok.

I have an idea then. Create 2 css classes in the <head> section of your aspx page.

So lets say one is lbStyle1 with the width set to 150px and the other is lbStyle2 and set the width to auto which means the browser will calculate the width.

E.g.

<style type="text/css">
lbStyle1
{
width: 150px;
}

lbStyle2
{
width: auto;
}
</style>

Then in your codebehind whereever you need it you can add something like this to change the CssClass of the listbox accordingly:

ListBox1.CssClass = lbStyle2; (in C#)
ListBox1.CssClass = lbStyle2 (in VB.Net)

HTH
-M3mph15
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 24294889
Thanks.  I ended up using a checkbox list control, which suited my needs.  But I gave u the points...  :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

580 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