【问题标题】:pass value of listbox items to select parameter将列表框项的值传递给选择参数
【发布时间】:2014-09-15 16:38:41
【问题描述】:

我有一个带有多列表框的表单:

<asp:ListBox ID="lstAction" runat="server" Rows="3" SelectionMode="Multiple"></asp:ListBox>

我有一个从对象数据源获取数据的存储过程,当传递一个值时它可以正常工作

<asp:ObjectDataSource ID="odsSalesSC" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="RapidFire.RFTableAdapters.USP_TOTAL_ORDERCALLSTableAdapter" OnSelecting="odsSalesSC_Selecting">
        <SelectParameters>
            <asp:ControlParameter ControlID="lstAction" Name="action" PropertyName="SelectedValue" Type="String" ConvertEmptyStringToNull="true" />                
        </SelectParameters>
    </asp:ObjectDataSource>

但是当用户选择多个选项时,我已经从选择中构建了一个字符串,该字符串传递到一个存储过程中,例如 ('a','b','c') 中的某些内容。 如何从 strAction 获取值以传递给我的选择参数?

protected void btnFilter_Click(object sender, EventArgs e)
    {            
        String strAction = String.Empty;           
        foreach (ListItem liAction in lstAction.Items)
        {
            if (liAction.Selected)
            {
                strAction += "'" + liAction.Value + "',";
            }
        }
        strAction = strAction.Substring(0, strAction.Length - 1);

【问题讨论】:

  • 请检查 Storedprocedure 它应该在 where 条件中查询 ('a','b') 而不是 = 。如果这解决了您的问题,请告诉我,我会将其添加为答案

标签: c# listbox objectdatasource


【解决方案1】:
string strYourIDs = "";

int[] yourSelectedIndexes = ListBox.GetSelectedIndices();
for (int i = yourSelectedIndexes.Length - 1; i >= 0; i--)
{
    strYourIDs += ListBox.Items[yourSelectedIndexes[i]].Value + ",";
}

if (strYourIDs != "")
    strYourIDs = strYourIDs.TrimEnd(",".ToCharArray());

从列表框中获取选定的索引,然后循环并将它们添加到逗号分隔的 strYourIDs 中。然后切断字符串末尾的最后一个逗号。现在,如果选择了三个值为 1、2 和 3 的项目,则字符串看起来像“1,2,3”。您可以通过创建拆分字符串函数或拆分这些值的方法在 SQL 中使用该字符串,您可以随意使用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2014-05-07
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多