在我的带有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() + " ";
}*/
然后Label 4正确显示ListBox中我希望我的ListBox显示的每个项目!所以,我知道ListItems正在被正确地添加到ListBox中;我只是无法理解为什么他们没有在列表框中显示任何文本。有任何想法吗??感谢
答案 0 :(得分:1)
从您的代码中,问题是showStuff
正在true
。您正在手动将项添加到ListBox,然后在非数据绑定列表框中调用ListBox.DataBind
,这将清空它的内容(因为AppendDataBoundItems
未设置为true
)。设置DataSource
,DataValueField
和DataTextField
,或停止数据绑定,ListBox应按预期运行。
答案 1 :(得分:0)
好的,我会假设这只是一些奇怪的错误。为了解决这个问题,我刚刚制作了一个新的模态弹出扩展器,一个新面板,并更改了列表框和模态弹出扩展器上的名称。现在它工作得很好......真的很奇怪,但它现在有效。