动态下拉列表无法按字母顺序排序

时间:2018-06-07 12:20:50

标签: c# asp.net alphabetical cascadingdropdown

我创建了2个下拉列表,第二个是第一个。我希望第二个的结果按字母顺序显示,但是,即使我在我的SQL中包含了ORDER BY,列表也没有生成排序列表。

  <div class="auto-style17">
            Please select the category of activity you are reviewing from the pick list :&nbsp; 
        <asp:SqlDataSource ID="iSAMS" runat="server" ConnectionString="<%$ ConnectionStrings:iSAMSConnectionString %>" 
            SelectCommand="SELECT [blnActive], [TblActivityManagerFolderID], [txtName] FROM [TblActivityManagerFolder] WHERE ([intActivity] = @intActivity)">
        <SelectParameters>
            <asp:Parameter DefaultValue="34" Name="intActivity" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>                     

    <asp:DropDownList ID="CategorySelect" runat="server" DataTextField="txtName" DataValueField="TblActivityManagerFolderID" 
        OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged" CssClass="newStyle1" AutoPostBack="True">
    </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CategorySelect" 
                ErrorMessage="Please select your answer" style="text-align: left; font-weight: 700; color: #FF0000; font-size: medium;">!</asp:RequiredFieldValidator>
        </div>

       <br />
         <div class="auto-style20">
            Please select the activity undertaken from the pick list:&nbsp; 
            <asp:DropDownList ID="ActivitySelect" runat="server" DataTextField="txtName" DataValueField="TblActivityManagerGroupId" CssClass="newStyle1" AutoPostBack="True">
    </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ActivitySelect" 
                ErrorMessage="Please select your answer" style="text-align: left; font-weight: 700; color: #FF0000; font-size: medium;">!</asp:RequiredFieldValidator>
            <asp:SqlDataSource ID="iSAMSActivity" runat="server" ConnectionString="<%$ ConnectionStrings:iSAMSConnectionString %>" SelectCommand="SELECT txtName, intActivity, intFolder, TblActivityManagerGroupId FROM TblActivityManagerGroup WHERE (intFolder = @intFolder) ORDER BY txtName ASC">
                <SelectParameters>
                    <asp:ControlParameter ControlID="CategorySelect" Name="intFolder" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
        </asp:SqlDataSource>                    
            </div>
            <div class="auto-style19">

任何想法如何让这个工作 - 请记住我是C#的新手,这是我的第一个项目所以我正在学习,所以我不知道在哪里放置编码解决方案。< / p>

这是背后的代码:

 protected void ActivitySelect_SelectedIndexChanged(object sender, EventArgs e)
{
    ActivitySelect.Items.Clear();
    ActivitySelect.Items.Add(new ListItem("--Select Activity--", ""));

    ActivitySelect.AppendDataBoundItems = true;
    String strConnString = ConfigurationManager
        .ConnectionStrings["iSAMSConnectionString"].ConnectionString;
    String strQuery = "select txtName, TblActivityManagerGroupID from dbo.TblActivityManagerGroup " +
                       "where intFolder=@intFolder";
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.Parameters.AddWithValue("@intFolder",
        CategorySelect.SelectedItem.Value);
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;
    try
    {
        con.Open();
        ActivitySelect.DataSource = cmd.ExecuteReader();
        ActivitySelect.DataTextField = "txtName";
        ActivitySelect.DataValueField = "TblActivityManagerGroupID";
        ActivitySelect.DataBind();
        if (ActivitySelect.Items.Count > 1)
        {
            ActivitySelect.Enabled = true;
        }
        else
        {
            ActivitySelect.Enabled = false;
        }
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}

}

1 个答案:

答案 0 :(得分:0)

根据建议,标记中声明的SqlDataSource根本不使用,但查询是从后面的代码创建的。您必须在后面的代码中更改ActivitySelect_SelectedIndexChanged()方法中的SQL语句:

String strQuery = "select txtName, TblActivityManagerGroupID from dbo.TblActivityManagerGroup " +
   "where intFolder=@intFolder order by txtName ";

或者,使用SqlDataSource上的DataSourceID属性更改代码以实际使用标记中的DropDownList,以链接到正确的SqlDataSource