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

listbox in usercontrol

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.