【问题标题】:Adding a "Select All" in drop down list在下拉列表中添加“全选”
【发布时间】:2016-02-13 09:37:30
【问题描述】:

我在 ddlCategory 中添加了一个“全选”值,以便在选择时选择所有类别,但我收到此错误消息“无法对 System.Int32 和 System.String 执行 '=' 操作。”有任何想法吗?提前致谢。

protected void Page_Load(object sender, EventArgs e)
    {
        if (ddlCategory.SelectedItem.Text == "Select All")
        {
            ProductDataSource.SelectCommand = "SELECT [ProductId], [ProductName], [ImageUrl], [Price], [CategoryId] FROM [Product] WHERE [SystemId] = 1";
        }
    }

这是 ProductDataSource 的代码:

<asp:SqlDataSource 
                   ID="ProductDataSource" 
                   EnableCaching="true" 
                   DataSourceMode="DataSet" 
                   runat="server" 
                   ConnectionString="<%$ ConnectionStrings:ProjectConnectionString1 %>" 
                   SelectCommand="SELECT [ProductId], [ProductName], [ImageUrl], [Price], [CategoryId] FROM [Product] WHERE       [SystemId] = 1" 
                   FilterExpression="CategoryId = '{0}'">
            <FilterParameters>
                <asp:ControlParameter Name="categoryparm" ControlID="ddlCategory" PropertyName="SelectedValue" />
            </FilterParameters>
        </asp:SqlDataSource>

【问题讨论】:

  • 你从哪里得到错误?
  • 我看不到任何会导致该错误的东西
  • 是的,代码中没有错误,但是当我运行它时,会显示该错误消息。我猜它的选择语句? @shree.pat18
  • Product表中SystemId列的数据类型是什么?
  • 我建议你尝试调试并单步调试你的代码,看看哪一行抛出了错误。

标签: c# sql asp.net .net datasource


【解决方案1】:

您添加了元素全选,但您的元素的价值是什么?我认为您最好使用 SelectedValue。添加到您的 DropDownList 元素 Select All 值为 0,然后您的方法将看起来像

if (ddlCategory.SelectedValue == 0){ /* your query here */}

另外,我建议您在页面首次加载时执行此操作,而不是在回发时执行。所以你的 Page_Load 方法看起来像

if (!IsPostBack){
    if (ddlCategory.SelectedValue == 0)
        ProductDataSource.SelectCommand = "select * from product where systemid = 1";
}

【讨论】:

  • 我试过了,但我在 ddlCategory 下得到那个错误行,说运算符'=='不能应用于'string'和'int'类型的操作数@khazratbek
  • Convert.ToInt32(ddlCategory.SelectedValue) == 0
  • @RonT 试试这个。希望对你有帮助
  • 还没有运气。我想知道它是否与 ProductDataSource 有关系。我刚刚在上面发布了我的代码。 @Khazratbek
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多