如何使用MS SQL数据库选择级联DropDownList

时间:2015-06-09 05:35:08

标签: c# asp.net sql-server

IDVehicle Make        Model
   0       BMW      5 series
   1       BMW      4 series
   2       AUDI        A4
   3       AUDI        A6
   4       TOYOTA      vios
   5       TOYOTA      RAV4

在c#设计器视图中,我有2个下拉列表,其中包含Make和其他模型。

我想要的是当用户在Make列表中选择'BMW'时,Model列表应该包含以下5 series, 4 series

ASPX.CS

Make: <asp:DropDownList ID="<MakeList" runat="server" /><br/>
Model: <asp:DropDownList ID="ModelList" runat="server"/><br />
<ajaxToolkit:CascadingDropDown ID="ccd1" runat="server"
    ServicePath="CascadingDropdown1.cs.asmx"
    ServiceMethod="GetMake"
    TargetControlID="MakeList" Category="Make"
    PromptText="Select Make"/>

<ajaxToolkit:CascadingDropDown ID="ccd2" runat="server"
    ServicePath="CascadingDropdown1.cs.asmx"
    ServiceMethod="GetModelForMake"
    TargetControlID="ModelList" ParentControlID="MakeList"
    Category="Model"
    PromptText="Select Model"/>

我的问题是我对查询很困惑,列名将在.cs.asmx文件中使用。

修改

我在这里做错了吗?

[WebMethod]
    public CascadingDropDownNameValue[] GetMakes(string knownCategoryValues)
    {
        string query = "SELECT VehicleMake, VehicleMakeId FROM Makes";
        List<CascadingDropDownNameValue> Makes = GetData(query);
        return Makes.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetModels(string knownCategoryValues)
    {
        string make = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["MakeId"];
        string query = string.Format("SELECT VehicleModel, VehicleModelId FROM Models WHERE MakeId = {0}", make);
        List<CascadingDropDownNameValue> Models = GetData(query);
        return Models.ToArray();
    }

    private List<CascadingDropDownNameValue> GetData(string query)
    {
        string conString = System.Configuration.ConfigurationManager.ConnectionStrings["SERVER=xbetasql,52292;UID=AutoTrakker;Password=trinidad2win;DATABASE=ATDBSQL;"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        using (SqlConnection con = new SqlConnection(conString))
        {
            con.Open();
            cmd.Connection = con;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    values.Add(new CascadingDropDownNameValue
                    {
                        name = reader[0].ToString(),
                        value = reader[1].ToString()
                    });
                }
                reader.Close();
                con.Close();
                return values;
            }
        }
    }

我遇到了错误。它说[Method Error 500]

更新

是否可以不为每列添加ID?

1 个答案:

答案 0 :(得分:3)

尝试这样的事情:

<asp:DropDownList ID="ddlMake" runat="server" Width="150"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cdlMake" TargetControlID="ddlMake" PromptText="Select Make" PromptValue="" ServicePath="CascadingDropdown1.asmx" ServiceMethod="GetMakes" runat="server" Category="VehicleMakeId" LoadingText="Loading Make..." />

<asp:DropDownList ID="ddlModel" runat="server" Width="150"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cdlModel" TargetControlID="ddlModel" PromptText="Select Model" PromptValue="" ServicePath="CascadingDropdown1.asmx" ServiceMethod="GetModels" runat="server" Category="VehicleModelId" ParentControlID="ddlMake" LoadingText="Loading Model..." />

CascadingDropDown属性

以下是AJAX CascadingDropDown的一些重要属性

  • TargetControlID - 这里我们需要设置你想要制作AJAX Cascading DropDownList的DropDownList控件的ID。

  • ServicePath - 这里我们设置Web服务的URL,它将作为AJAX CascadingDropDown DropDownList的数据源。

  • ServiceMethod - 这里我们设置将用于填充AJAX CascadingDropDown DropDownList的Web方法的名称。

  • PromptText - 此属性是将在AJAX CascadingDropDown DropDownList中显示的第一个或默认项的Text部分。

  • PromptValue - 此属性是将在AJAX CascadingDropDown DropDownList中显示的第一个或默认项的Value部分。

  • 类别 - 此属性用于指定AJAX CascadingDropDown DropDownList的类别,类别值作为参数传递给Child或从属AJAX CascadingDropDown DropDownList ServiceMethod,即Web方法。

  • ParentControlID - 此属性用于设置DropDownList的ID,DropDownList将在其选择中触发数据填充过程。

  • LoadingText-此属性用于在调用Web方法时显示加载文本,直到在AJAX CascadingDropDown DropDownList中填充数据。

让您的WebMethods如下:

[WebMethod]
public CascadingDropDownNameValue[] GetMakes(string knownCategoryValues)
{
    string query = "SELECT VehicleMakeName, VehicleMakeId FROM Makes";
    List<CascadingDropDownNameValue> Makes = GetData(query);
    return Makes.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetModels(string knownCategoryValues)
{
    string make = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["MakeId"];
    string query = string.Format("SELECT VehicleModelName, VehicleModelId FROM Models WHERE MakeId = {0}", make);
    List<CascadingDropDownNameValue> Models = GetData(query);
    return Models.ToArray();
}

private List<CascadingDropDownNameValue> GetData(string query)
{
   string conString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();
        cmd.Connection = con;
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                values.Add(new CascadingDropDownNameValue
                {
                    name = reader[0].ToString(),
                    value = reader[1].ToString()
                });
            }
            reader.Close();
            con.Close();
            return values;
        }
    }
}