我想要做的是从一个多选列表框中取出所有选定的项目,并将它们放在一个逗号分隔的字符串中,这样我就可以将它存储在一个表中。我已经搜索并找到了代码,但由于某种原因,从未发现限定符是“真实的”。它将每个选定的项目视为“假”。我是否以错误的顺序处理这个?
这是我的 ASP 部分(因为它是一个巨大的文件而被剪掉,但这是重要的东西):
<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PBR.WebForm1" MaintainScrollPositionOnPostback="true"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AJAXControls" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<link rel="stylesheet" href="Styles/ui.all.css" type="text/css" media="screen" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<asp:ScriptManager ID="ScriptManager2" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanelX" runat="server" UpdateMode="Conditional" Height="390px"
Width="900px" BorderStyle="Groove" BorderWidth="2px">
<ContentTemplate>
<AJAXControls:TabContainer runat="server" ID="tabContainer" Height="373" Width="900" >
<AJAXControls:TabPanel ID="secondTab" HeaderText="Tracking Page 2" runat="server">
<ContentTemplate>
<div style="border:1px solid blue;">
<asp:Panel ID="Panel2" runat="server" Height="40px" style="margin-left: 19px"
Width="860px">
<table>
<tr>
<td width="170">System/Document Change:</td>
<td width="30"><asp:ListBox id="ddlSysDocChg" runat="server" Width="90px" Rows="2" SelectionMode="Multiple"></asp:ListBox></td>
<td width="40"></td>
<td width="200">System/Document Change Completed:</td>
<td width="20"><asp:CheckBox ID="chkSysDocChg" runat="server" Text=" " AutoPostBack="true" /></td>
</tr>
</table>
</asp:Panel>
</div>
<p></p>
<div>
</div>
</ContentTemplate>
</AJAXControls:TabPanel>
</AJAXControls:TabContainer>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_OnClick"
runat="server" />
</div>
</asp:Content>
在我的代码隐藏中,我有这个(如你所见,我尝试了两种不同的方法,我相信我在这个网站上找到了这两种方法):
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
// Read the selected items from the listbox
//string SQLCode = "";
var selectedQuery = ddlSysDocChg.Items.Cast<ListItem>().Where(item => item.Selected);
string SQLCode = String.Join(",", selectedQuery).TrimEnd();
//foreach (ListItem listitem in ddlSysDocChg.Items)
// {
// if (listitem.Selected == true)
// {
// SQLCode = SQLCode + ", " + listitem;
// }
// }
}
谁能告诉我为什么它总是告诉我没有选择?
编辑:这是我的 Page_Load 中的内容:protected void Page_Load(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(str)) // 检查哪些选项卡应该处于活动状态 LoadTabPages(); {
try
{
string strSQL = "SELECT ComboValue, ComboText FROM dbo.tblComboBoxes WHERE ComboName = 'ddlSysDocChg' ORDER BY ComboText ASC;";
SqlDataAdapter adapter = new SqlDataAdapter(strSQL, str);
DataSet DailyRun = new DataSet();
adapter.Fill(DailyRun);
ddlSysDocChg.DataSource = DailyRun;
ddlSysDocChg.DataTextField = "ComboText";
ddlSysDocChg.DataValueField = "ComboValue";
ddlSysDocChg.DataBind();
foreach (ListItem item in ddlSysDocChg.Items)
{
item.Attributes.Add("Title", item.Text);
}
// Insert a blank row into the DropDownLists so there is no default name
ddlSysDocChg.Items.Insert(0, new ListItem("", ""));
}
catch (Exception ex)
{
// Handle the error
Console.WriteLine("Making Call to " + ex + "");
}
}
}