简体   繁体   English

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

[英]How to select cascading DropDownList with MS SQL database

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

In the c# designer view, I have 2 drop-down list one which has Make and other for Model. 在c#设计器视图中,我有2个下拉列表,其中包含Make和其他模型。

What I want is that when user picks 'BMW' in Make list, the Model list should have the following 5 series, 4 series 我想要的是当用户在Make列表中选择'BMW'时,Model列表应该包含以下5 series, 4 series

ASPX.CS 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"/>

my problem is that I'm so confused about the query and the column name will be used in the .cs.asmx file. 我的问题是我对查询很困惑,列名将在.cs.asmx文件中使用。

EDIT 编辑

Am i Doing wrong in here ? 我在这里做错了吗?

[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;
            }
        }
    }

I am having an error. 我有一个错误。 it says [Method Error 500] 它说[Method Error 500]

UPDATE UPDATE

Is it ok not to have an ID for each column? 是否可以不为每列添加ID?

Try something like this: 尝试这样的事情:

<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 Properties CascadingDropDown属性

Below are some important properties of the AJAX CascadingDropDown 以下是AJAX CascadingDropDown的一些重要属性

  • TargetControlID – Here we need to set the ID of the DropDownList control for which you want to make an AJAX Cascading DropDownList. TargetControlID - 这里我们需要设置要为其创建AJAX Cascading DropDownList的DropDownList控件的ID。

  • ServicePath – Here we set the URL of the Web Service that will act as source of data for the AJAX CascadingDropDown DropDownList. ServicePath - 这里我们设置Web服务的URL,它将充当AJAX CascadingDropDown DropDownList的数据源。

  • ServiceMethod – Here we set the name of the Web Method that will be used to populate the AJAX CascadingDropDown DropDownList. ServiceMethod - 这里我们设置将用于填充AJAX CascadingDropDown DropDownList的Web方法的名称。

  • PromptText – This property is the Text part that of the first or the default item that will be displayed in the AJAX CascadingDropDown DropDownList. PromptText - 此属性是将在AJAX CascadingDropDown DropDownList中显示的第一个或默认项的Text部分。

  • PromptValue – This property is the Value part that of the first or the default item that will be displayed in the AJAX CascadingDropDown DropDownList. PromptValue - 此属性是将在AJAX CascadingDropDown DropDownList中显示的第一个或默认项的Value部分。

  • Category – This property is used to specify the Category for the AJAX CascadingDropDown DropDownList, Category value is passed as parameter to the Child or dependent AJAX CascadingDropDown DropDownList ServiceMethod ie Web Method. Category - 此属性用于指定AJAX CascadingDropDown DropDownList的Category,Category值作为参数传递给Child或依赖AJAX CascadingDropDown DropDownList ServiceMethod即Web方法。

  • ParentControlID – This property is used to set the ID of the DropDownList on whose selection the DropDownList will trigger the data population process. ParentControlID - 此属性用于设置DropDownList的ID,DropDownList将在其选择中触发数据填充过程。

  • LoadingText– This property used to display the loading text when the call is made to the Web Method until the data is populated in the AJAX CascadingDropDown DropDownList. LoadingText-此属性用于在调用Web方法时显示加载文本,直到在AJAX CascadingDropDown DropDownList中填充数据。

And have your WebMethods as follows: 并将您的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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM