0

リストボックスを使用した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 に正しく追加されていることはわかっています。リストボックスにテキストが表示されない理由がわかりません。何か案は??ありがとう

4

2 に答える 2

1

あなたのコードから、問題showStufftrue. ListBox に項目を手動で追加してから、ListBox.DataBindデータバインドされていないリスト ボックスを呼び出すと、その内容が空になります (AppendDataBoundItemsが に設定されていないためtrue)。DataSourceDataValueField、および を設定するかDataTextField、データバインディングを停止すると、ListBox が期待どおりに機能するはずです。

于 2012-08-14T18:18:33.647 に答える
0

さて、これは起こった奇妙なバグだと思います。これを解決するために、新しいモーダルポップアップエクステンダーと新しいパネルを作成し、リストボックスとモーダルポップアップエクステンダーの名前を変更しました。今では問題なく動作します...本当に奇妙ですが、今は動作します。

于 2012-08-15T21:40:38.150 に答える