求高手指点 ASP.NET ----ListBox控件问题?? 
在ListBox控件中怎么获取选种行?
ListBox1.SelectedItem.Text;获取不到值,为什么?
<asp:ListBox ID="ListBox1" runat="server" Height="344px" 
            OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
                Width="409px"></asp:ListBox>public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string dir = @"G:\PHOTO";
        ListBox1.DataSource = ListFiles(new DirectoryInfo(dir));
        ListBox1.DataBind();
    }
    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 == ".jpg")
            {
                al.Add(file.FullName.Substring(file.FullName.LastIndexOf(@"\") + 1));
            }
        }
        Array a = al.ToArray();
        Array.Sort(a);
        Array.Reverse(a);
        return a;
    }   
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = ListBox1.SelectedIndex; ;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = ListBox1.SelectedItem.Text;
    }
}

解决方案 »

  1.   

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

  2.   

    hehe,给了代码总要自己修饰一下的...一般数据绑定的时候在Page_Load里总是要用
    if(!IsPostBack)
    判断一下的下次你第一个帖子说清楚,就不用再发第二个了...
      

  3.   

    如果你要用ListBox1_SelectedIndexChanged时间,记得ListBox1的AutoPostBack属性要设置为true