简体   繁体   English

级联下拉列表未填充

[英]Cascading dropdown not populating

I have been following the following example to create some cascading dropdowns in a web application. 我一直在遵循以下示例在Web应用程序中创建一些级联下拉列表。 The example does not use jquery and I would like to stick with this for now. 该示例不使用jquery,我现在想坚持使用它。 https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx

I am having trouble getting my second dropdown to populate a list using the result from the first. 我无法使用第二个下拉列表中的第一个结果来填充列表。 Here is my code: 这是我的代码:

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CategorySelect.Items.Clear();
       CategorySelect.Items.Add(new ListItem("--Select Activity--", ""));

        CategorySelect.AppendDataBoundItems = true;
        String strConnString = ConfigurationManager
            .ConnectionStrings["iSAMSConnectionString"].ConnectionString;
        String strQuery = "select TblActivityManagerFolderID, txtName from dbo.TblActivityManagerFolder";
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            CategorySelect.DataSource = cmd.ExecuteReader();
            CategorySelect.DataTextField = "txtName";
            CategorySelect.DataValueField = "TblActivityManagerFolderID";
            CategorySelect.DataBind();
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
}
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=@txtName";
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.Parameters.AddWithValue("@txtName",
        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();
    }
}

I am using 2 tables. 我正在使用2张桌子。

TblActivityManagerFolder where I am using 2 fields - TblActivityManagerFolderID and txtName TblActivityManagerFolder这里我使用的2场- TblActivityManagerFolderIDtxtName

TblActivityManagerGroup where I am using 2 fields - intFolder (which joins to TblActivityManagerFolderID) and txtName TblActivityManagerGroup这里我使用的2场- intFolder (其连接到TblActivityManagerFolderID)和txtName

My first drop down populates exactly as expected, but when I make a selection nothing at all happens in the 2nd dropdown. 我的第一个下拉列表完全按预期填充,但是当我进行选择时,第二个下拉列表中什么也没有发生。

Additional Code: 附加代码:

<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>
                </div>

            <div class="auto-style18">

        <asp:DropDownList ID="CategorySelect" runat="server" DataTextField="txtName" DataValueField="TblActivityManagerFolderID" 
            OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged" CssClass="newStyle1">
        </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-style19">
                <div class="auto-style20">
                Please select the activity undertaken from the pick list:&nbsp; 
                <asp:DropDownList ID="ActivitySelect" runat="server" DataSourceID="iSAMSActivity" DataTextField="txtName" DataValueField="TblActivityManagerGroupId" CssClass="newStyle1" OnSelectedIndexChanged="ActivitySelect_SelectedIndexChanged">
        </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)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="CategorySelect" Name="intFolder" PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
            </asp:SqlDataSource>                    

I am also posting back the results so I can see if this is working using: 我还回发了结果,因此我可以使用以下方法查看它是否在工作:

protected void Button1_Click(object sender, EventArgs e)
    {
        var activity = CategorySelect.DataTextField;
        var CoCActivity = ActivitySelect.DataTextField;
        var OtherSkills = SkillsTextBox.Text;
        var ExtraDev = DevTextBox.Text;
        var OtherActivities = OtherActivitiesTextBox.Text;
        OutputLabel.Text = activity + "<br/>" + CoCActivity + "<br/>" + OtherSkills + "<br/>" + ExtraDev + "<br/>" + OtherActivities;
    }

When it is posting back, it is producing an integer rather than the label in the dropdown list. 回发时,它会生成一个整数,而不是下拉列表中的标签。 Can this be changed so it post backs the actual text? 可以更改它以便回发实际文本吗?

Set the AutoPostBack property of the DropdownList to True. AutoPostBack属性设置为True。 If you dont set it, the event would be only be triggered server side when other events force a postback (a button click or similar) 如果未设置,则仅当其他事件强制回发(单击按钮或类似操作)时,才在服务器端触发该事件

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

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