在我的带有 Listbox 的 asp.net 应用程序中,最奇怪的事情发生在我身上——ListItems 没有显示它们的文本。我知道它们在那里,因为当我设置断点时我可以看到它们,最重要的是,我使用的代码几乎与我在另一个页面上显示的代码完全相同。我的列表框位于一个由模式弹出窗口调用的面板上,如下所示:
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
DynamicServicePath="" Enabled="True" TargetControlID="Button5"
BackgroundCssClass="modalBackground"
DropShadow="True"
PopupControlID="Panel2" CancelControlID="Button5" OkControlID="Button5">
</asp:ModalPopupExtender>
<asp:Panel ID = "Panel2" runat="server" CssClass="modalPopup">
<table>
<tr>
<td class="style3">
<asp:Label ID="Label5" runat="server" Text="The following users are queued front of you. Select 'OK' to add your name to the queue and 'Cancel' to cancel."></asp:Label>
</td>
</tr>
</table>
<asp:ListBox ID="ListBox6" runat="server" Width="100%"></asp:ListBox>
<center>
<asp:Button ID="reqOk" runat="server" Text="OK" onclick="reqOk_Clk" />
<asp:Button ID="reqCncl" runat="server" Text="Cancel" onclick="reqCncl_Clk" />
</center>
</asp:Panel>
列表框(ListBox6)的填充就像在页面后面的代码中一样:
SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings
["something"].ConnectionString);
SqlCommand sqlcommand = new SqlCommand();
sqlcommand.Connection = sqlconn;
string cmd = "";
cmd = " SELECT devices.requestQueue, devices.invnumber FROM devices WHERE devices.invnumber='" + str + "'";
sqlcommand.CommandText = cmd;
SqlDataAdapter da = new SqlDataAdapter(sqlcommand);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
object[] obj = dt.Rows[0].ItemArray;
sqlconn.Close();
hasRequest = true;
int i2 = 0;
if (!obj[0].Equals(System.DBNull.Value))
{
string see = (string)obj[0];
string[] words = see.Split(',');
foreach (string word in words)
{
i2++;
if (word.Contains(getname(HttpContext.Current.User.Identity.Name)))
{
showStuff = false;
ListItem item = new ListItem(word);
ListBox6.Items.Add(item);
ModalPopupExtender1.Show();
Label5.Text = "You have already requested " + (string)ViewState["inventory"] + ". Please press cancel and request a different device.";
reqOk.Visible = false;
}
else
{
ListItem item = new ListItem(word);
ListBox6.Items.Add(item);
}
}
}
/*foreach (object o in ListBox6.Items)
{
Label4.Text += o.ToString() + " ";
}*/
if (showStuff == true)
{
ListBox6.DataBind();
if (obj[0].Equals(System.DBNull.Value) || (string)obj[0] == "")
i2 = 0;
Label5.Text = "The following " + i2 + " user(s) are queued in front of you for device " + str + ". Select 'OK' to add your name to the queue and 'Cancel' to cancel. Your name will not be added to the queue if you select 'Cancel'.";
ModalPopupExtender1.Show();
让这超级令人沮丧的是,当我从上面取消注释这段代码时:
/*foreach (object o in ListBox6.Items)
{
Label4.Text += o.ToString() + " ";
}*/
然后标签 4 正确显示了我希望我的 ListBox 显示的 ListBox 中的每个项目!所以,我知道 ListItems 被正确添加到 ListBox 中;我只是不明白为什么他们没有在列表框中显示任何文本。有任何想法吗??谢谢