473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

listbox in usercontrol

hi,everyone.
I have a question about using listbox in a customized user control.I use
a listbox directly in a asp.net page and it works very well,but when I use
the same code in a user control,things get different. My code is listed
below:
//declare listbox in the user control ascx file:

<asp:ListBox ID="ListBox1" runat="server" Height="344px"
OnSelectedIndexChanged="ListBox1_SelectedIndexChan ged"
Width="409px"></asp:ListBox>

//bind the listbox in the user control's page_load event:
string dir = @"e:\work\¹¤×÷Îĵµ";

if (!IsPostBack)
{
ListBox1.DataSource = ListFiles(new DirectoryInfo(dir));
ListBox1.DataBind();
}

//the codes that realize ListFile method
public Array ListFiles(DirectoryInfo dir)
{
if (!dir.Exists)
return null;

if (dir == null)
return null;

FileSystemInfo[] files = dir.GetFileSystemInfos();
ArrayList al = new ArrayList();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if (file != null && file.Extension == ".doc")
{
al.Add(file.FullName.Substring(file.FullName.LastI ndexOf(@"\")
+ 1));
}
}
Array a = al.ToArray();
Array.Sort(a);
Array.Reverse(a);
return a;
}

phenomenon: when I put these code in my user control to use this
listbox, when the page is postbacked,the content in listbox is empty. Can
anyone help me? if I don't use "if( !ispostback) ",the content of listbox
can be seen again,but another problem is when I select any item in the
listbox,the selected status can not be preserved after another postback
event occur.

Thanks anyway!
Crespo

2007-06-07
Jun 7 '07 #1
0 1423

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
1
by: Peter Osawa | last post by:
Hi, how can I insert an UserControl as an item in a listbox... I need this because the amount of UCs displayed isn't alwyas the same, and I think that displaying UCs in a listbox is the cleanest...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
3
by: ted | last post by:
Hi, I can't figure out how to set multiple items in a ListBox to selected when the listbox is in a User Control. When the ListBox is set in directly into a form I can use the following:...
4
by: Fernando Lopes | last post by:
Hi. I have a page with a usercontrol. In this UC, I have two ListBoxes and two buttons. When i click in one button, i get the selected item in the listbox. How can i validate, using validators,...
3
by: HDI | last post by:
Hi, How can i make a listbox where I can add a usercontrol at run time? Thx
5
by: markr1000 | last post by:
I must have looked searched in 500+ places that showed up in Google searchs, but not one has an example of what I want to do. I have a Listbox on a User Control because I want to control the...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
6
by: kucheravy | last post by:
Hi, everybody. I have this problem. When I put a <asp:ListBox on a web page and populate the data in the page Page_Load event the ViewState for the control is saved and loaded (after postback)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.