twinPrimesAreEz
twinPrimesAreEz

Reputation: 125

ListItems text not showing in Listbox

The strangest thing happened to me in my asp.net application with a Listbox -- the ListItems are not showing their text. I know they are there because I can see them when I set breakpoints, and on top of that I am using almost the exact same code as I am on another page where they show up just fine. My listbox is on a panel that is called by a modal pop-up that looks like this:

<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>

The listbox (ListBox6) is populated like in the code behind page like this:

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();

What makes this super frustrating is that when I uncomment this peice of code from above:

/*foreach (object o in ListBox6.Items)
{
  Label4.Text += o.ToString() + " ";
}*/

then Label 4 properly shows each of the items in the ListBox that I want my ListBox to be showing! So, I know the ListItems are being added to the ListBox correctly; I just cannot understand why they are not showing any text in the listbox. Any ideas?? Thanks

Upvotes: 0

Views: 2198

Answers (2)

twinPrimesAreEz
twinPrimesAreEz

Reputation: 125

Ok, I am going to assume this is just some weird bug that happened. To resolve this I just made a new modal popup extender, a new panel and changed the name on the listbox and the modal popup extender. Now it works just fine... Really weird but it works now.

Upvotes: 0

Jaime Torres
Jaime Torres

Reputation: 10515

From your code, the problem is stemming showStuff being true. You are adding items manually to your ListBox, then calling ListBox.DataBind on a non-databound list box, which will empty it's contents (since AppendDataBoundItems is not set to true). Either set the DataSource, DataValueField, and DataTextField, or stop databinding and your ListBox should function as expected.

Upvotes: 1

Related Questions