在下拉列表中添加“全选”

时间:2015-11-12 05:36:05

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

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

1 个答案:

答案 0 :(得分:0)

您添加了元素全选,但元素的价值是多少?我认为你最好使用 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";
}