listbox in usercontrol

Discussion in 'ASP .Net' started by crespo, Jun 7, 2007.

  1. crespo

    crespo Guest

    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
     
    crespo, Jun 7, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huobazi
    Replies:
    1
    Views:
    742
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    444
    Rajesh Tiwari
    Aug 12, 2003
  3. nail
    Replies:
    0
    Views:
    1,581
  4. Phl
    Replies:
    2
    Views:
    548
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  5. Fabiano
    Replies:
    2
    Views:
    651
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
Loading...

Share This Page