下拉列表没有正确填充

时间:2015-03-09 14:36:20

标签: c# asp.net

我有一个Edit Account页面,人们可以通过状态,区域和公司下拉列表更新信息,这些列表预先填充数据。我遇到的问题是,当一个人选择一个不同的国家时,新选择国的地区不会填入地区下拉列表中;它实际上保留了前一个州的地区。如何让新选区的区域出现在区下拉列表中?非常感谢任何帮助。

ASPX

<asp:Panel ID="editAcctPanel" runat="server" DefaultButton="submitButton" ValidationGroup="EditAccount">

<asp:Label ID="stateLbl" runat="server" Text="State:" Visible="true"></asp:Label>
<asp:DropDownList ID="stateDDL" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="StateName" DataValueField="StateID" OnDataBound="stateDDL_DataBound" Width="205px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings %>"
SelectCommand="SELECT * FROM State"></asp:SqlDataSource>
<br />

<asp:Label ID="DistLbl" runat="server" Text="District:" Visible="true"></asp:Label>
<asp:DropDownList ID="districtDDL" runat="server" DataSourceID="SqlDataSource3" AutoPostBack="True" DataTextField="DistrictName" DataValueField="DistrictID" OnDataBound="districtDDL_DataBound" Width="65%">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings %>"
SelectCommand="SELECT DistrictID, DistrictName FROM District ORDER BY DistrictName">
</asp:SqlDataSource>
<br />

<asp:Label ID="CompanyLbl" runat="server" Text="Company:" Visible="true"></asp:Label>
<asp:DropDownList ID="companyDDL" runat="server" DataSourceID="SqlDataSource4" DataTextField="CompanyName" DataValueField="CompanyID" OnDataBound="companyDDL_DataBound" Width="65%">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings %>"
SelectCommand="SELECT CompanyID, CompanyName FROM Company ORDER BY CompanyName">
</asp:SqlDataSource>
<br />
<br />
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" ValidationGroup="EditAccount" />

</asp:Panel>

C#

protected void stateDDL_DataBound(object sender, EventArgs e)
{
    //stateDDL.Items.Insert(0, new ListItem("- Select a State -", "NA"));
    //stateDDL.SelectedValue = "AL";
}

protected void districtDDL_DataBound(object sender, EventArgs e)
{
    //districtDDL.Items.Insert(0, new ListItem("- Select a District -", "00"));
}

protected void companyDDL_DataBound(object sender, EventArgs e)
{
    //companyDDL.Items.Insert(0, new ListItem("- Select a Company -", "00"));
}
….
….
SqlDataReader reader = GetInfo.ExecuteReader();
        while (reader.Read())
        {
            stateDDL.SelectedValue = reader["StateIDNum"].ToString();
            if (reader["DistrictGroupIDNum"].Equals(1))
            {
                districtDDL.SelectedValue = reader["DistrictID"].ToString();
                companyDDL.SelectedValue = reader["CompanyIDNum"].ToString();
            }
            else
            {
                companyfNameTxtbox.Text = reader["FirstName"].ToString();
                companylNameTxtBox.Text = reader["LastName"].ToString();
                companyemailTxtBox.Text = reader["Email"].ToString();
            }

        }
        reader.Close();

    }
    finally
    {
        conn.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

在几个人的帮助下,我设法让学区下拉列表工作。

<强> ASPX
添加了OnSelectedIndexChanged

<asp:DropDownList ID="stateDDL" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="StateName" DataValueField="StateID" OnSelectedIndexChanged="stateDDL_OnSelectedIndexChanged" 
                Width="205px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings %>"
SelectCommand="SELECT * FROM State"></asp:SqlDataSource>

<强> C#
区的SqlDataSource3

protected void stateDDL_OnSelectedIndexChanged(object sender, EventArgs e)
{
    SqlDataSource3.SelectParameters.Clear();
    SqlDataSource3.SelectParameters.Add("@StateIDNum", "StateIDNum");
    SqlDataSource3.SelectParameters["@StateIDNum"].DefaultValue = stateDDL.SelectedValue;
    districtDDL.DataSourceID = "SqlDataSource3";
    districtDDL.DataBind();
}