By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,385 Members | 1,930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,385 IT Pros & Developers. It's quick & easy.

listbox in usercontrol

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

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

//bind the listbox in the user control's page_load event:
string dir = @"e:\work\ĵ";

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

//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();
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!

Jun 7 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.