如何使用C#为ASP.NET中的SqlDataSource中的参数赋值多个值 [英] how to assign multiple values to a parameter in SqlDataSource in ASP.NET using C#

查看:153
本文介绍了如何使用C#为ASP.NET中的SqlDataSource中的参数赋值多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有名为Category的字段的Sql数据源,它具有类别1,类别2等值。我想要一个gridview,用户可以选择在任何类别中选择值,或者为已归类的类别组合。



为用户提供此选择,我有一个值为category1和category2等的下拉列表。我选择类别的代码是

  protected   void  SqlDataSource1_Selecting( object  sender,SqlDataSourceSelectingEventArgs e)
{

if (CategoryDropdownList.SelectedValue = Category1
{
e.Command.Parameters [ @ Category]。值= 类别1\" ;
}
else if (CategoryDropdownList.SelectedValue = category2
{
e.Command.Parameters [ @ Category]。值= 类别2;
}
}



此代码在为字段类别选择值时工作正常。



现在我想为用户介绍一个多选项,他们可以选择多个类别



例如:如果下拉列表的值为category1or2 ,这个值的选择应该给出数据库中的用户值,其中Category字段的值是category1或ctegory2

我无法找到这种多选参数的代码。我也不确定是否可以通过使用'OR'和'AND'分配多个值,因为我们在if语句中使用。



请建议我我应该遵循什么样的行动。



如果这个问题听起来太基本,请原谅我提问并帮助我,因为我还是ASP.NET的初学者。我正在使用C#。

解决方案

http:// www。 w3schools.com/sql/sql_and_or.asp [ ^

I have an Sql Data Source with a field named Category which has values like category1, category 2 etc. I want a gridview where user has a choice to select valued in any category or a combination of categories for the filed Category.

for giving this choice to users, I have a dropdownlist with values category1 and category2 etc. My code for selecting a category is

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

        if(CategoryDropdownList.SelectedValue = "Category1")
            {
                e.Command.Parameters["@Category"].Value = "category1";
            }
        else if (CategoryDropdownList.SelectedValue = "category2")
        {
            e.Command.Parameters["@Category"].Value = "category2";
        }
}


This code works fine when selecting a value for a field Category.

Now I want to introduce a multiple selection choice for users whereby they can select more than one category

for example: if the dropdown list has an value category1or2, the selection of this value should give the user values from database where value of Category field is either category1 or ctegory2
I am not able to find the code for this kind of multiple choice of a parameter. I am also not sure whether it is possible to assign suck multiple values by use of 'OR' and 'AND' as we use in if statements.

Kindly advice me what course of action I should follow.

if this question sounds too elementary, kindly pardon me for asking and help me because I am still a beginner in ASP.NET. I am using C#.

解决方案

http://www.w3schools.com/sql/sql_and_or.asp[^]


这篇关于如何使用C#为ASP.NET中的SqlDataSource中的参数赋值多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆