【发布时间】:2016-01-12 22:36:32
【问题描述】:
我在这里遗漏了一些非常简单的东西...我已经通过了我的调试器并且一切正常,但是 ListBox 没有填充新数据。当我运行网站时它显示为空。
我想:
Step.1 使用我的数据库中的文件路径字符串填充列表框。 (没关系)
Step.2 使用 ListBox 项创建一个新列表,从进程中的每个字符串中删除路径。 (这似乎没问题,当我调试时,count 正在为list 增加)
Step.3 使用我的新“列表”重新填充列表框。 (不工作)
<div>
<asp:ListBox ID="ListBox1" runat="server" Width="100%" Height="100%" AutoPostBack="true"/>
</div>
从数据库中检索文件路径并填充 DataTable:(这没关系)
private DataTable loadUserImageNames()
{
string cpUsersConnection1 = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
SqlConnection oSqlConnection1 = new SqlConnection(cpUsersConnection1);
oSqlConnection1.Open();
SqlCommand oSqlCommand1 = new SqlCommand();
oSqlCommand1.Connection = oSqlConnection1;
oSqlCommand1.CommandType = CommandType.Text;
oSqlCommand1.CommandText = "SELECT FilePath from User_Images where AcNo ='" + AcNo.Text + "' ORDER BY AcNo";
SqlDataAdapter oSqlDataAdapter1 = new SqlDataAdapter();
oSqlDataAdapter1.SelectCommand = oSqlCommand1;
DataTable oDataTable1 = new DataTable("User_Images");
oSqlDataAdapter1.Fill(oDataTable1);
return oDataTable1;
}
将 DataTable 分配给 Listbox(可以)。
从 ListBox 中调用每个项目并删除 Path,只留下 finalFileName(这没关系)。
将finalFileName 分配给list(我认为这没问题,调试中“计数”正在增加)
将list 分配给ListBox1....这不起作用,ListBox1 为空。
if (!Page.IsPostBack)
{
DataTable oDataTable1 = loadUserImageNames();
ListBox1.DataSource = oDataTable1;
ListBox1.DataTextField = "FilePath";
ListBox1.DataBind();
List<string> list = new List<string>();
foreach (ListItem s in ListBox1.Items)
{
string filePath = (s.ToString());
string finalFileName = (filePath.Substring(filePath.LastIndexOf('/') + 1));
list.Add(finalFileName);
}
ListBox1.DataSource = list;
}
【问题讨论】:
-
您是否尝试添加 ListBox1.DataBind();在 ListBox1.DataSource = 列表之后; ?我认为这应该可以解决问题
-
我有,它让我进入 catch 语句,但我不知道为什么。 (有一个 try/catch 未显示)
-
什么异常?也尝试把 List
list = new List 之前();在 if...