c# - 无法在 ASP.NET 网络表单中获取所选项目
问题描述
我正在尝试使用 ASP.NET 网络表单制作文件资源管理器。但是当我试图在列表框中获取选定项目时,出现错误:
System.NullReferenceException
我试图在 Winforms 中制作它,并且效果很好。我很困惑。
这是一些网络表单代码:
public partial class Default : System.Web.UI.Page
{
DriveInfo[] drives;
string path = "";
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();
drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
ListBox1.Items.Add(d.Name);
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// this is the error section
path = ListBox1.SelectedItem.ToString();
ListBox1.Items.Clear();
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
foreach(string d in dirs)
{
ListBox1.Items.Add(d);
}
foreach(string f in files)
{
ListBox1.Items.Add(f);
}
TextBox1.Text = path;
}
}
我是网络表单的初学者,这是我的第一个项目。
如果这是一个愚蠢的问题,我很抱歉。
解决方案
推荐阅读
- javascript - 如何延迟 gif 从页面加载开始?
- garbage-collection - 如何理解“重新定位可从运行代码直接访问的对象”?
- python - 我想将我的第二个元组列表排列到另一个元组列表的第一个列表中
- sql - 如何检查 Select 语句返回多行
- neo4j - 比较 Neo4j 中的两个列表
- html - 可访问性 -和之间的区别
- android - MediaStore.MediaColumns.DATA 已弃用并且 contentResolver.openAssetFileDescriptor(uri, "r") 花费大量时间。如何检查 Uri 是否存在?
- streaming - 这种窗口模式中键的目的是什么?
- logstash - logstash 过滤器中的 Java Strace gork 模式
- android - Flaky Espresso 测试:启动活动后测试失败并超时