C
crespo
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_SelectedIndexChanged"
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 as FileInfo;
if (file != null && file.Extension == ".doc")
{
al.Add(file.FullName.Substring(file.FullName.LastIndexOf(@"\")
+ 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
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_SelectedIndexChanged"
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 as FileInfo;
if (file != null && file.Extension == ".doc")
{
al.Add(file.FullName.Substring(file.FullName.LastIndexOf(@"\")
+ 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