来自数据库

时间:2016-08-02 17:50:51

标签: c# sql asp.net

我有两张桌子;它们在数据库中为tblcitytblarea,如:

tblcity

city id | city
--------------
1       | ktm
2       | bkt
3       | lat

tblcity

areaid | cityid | area
----------------------
1      |    1   |  a
2      |    1   |  b
3      |    1   |  c
4      |    2   |  d
5      |    2   |  e
6      |    3   |  f
7      |    3   |  g

城市表由三个城市名称组成,即ktm,bkt,lat。表区域包含该城市区域的名称。

我在一个下拉列表中显示了所有城市名称,即DropDownList1。现在我想要做的是在另一个下拉列表中显示该城市的所有相应区域名称,即在DropDownList7中选择DropDownList1中的一个城市名称时。

就像在DropDownList1中选择城市ktm一样,只有区域名称如a,b,城市ktm的c应该应该显示在DropDownList7中。

现在的问题是,在这里我可以在DropDownList1中显示所有城市名称,但是在选择DropDownList1中的一个城市时,属于该城市的区域名称不会显示在DropDownList7中。

 <asp:DropDownList ID="DropDownList1" CssClass="form-control shadow2 input-lg" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        </asp:DropDownList>
                        <asp:DropDownList ID="DropDownList7" runat="server" Height="111px" Width="376px"></asp:DropDownList>

背后的代码

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        DataTable dt = c.getallcity();
        if (dt.Rows.Count>0)
        {
            DataRow dr = dt.NewRow();
            dr["city"] = "select city ";
            dt.Rows.InsertAt(dr, 0);
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "city";
            DropDownList1.DataValueField = "cityid";
            DropDownList1.DataBind();


        }


    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedIndex!= 0)
    {
        DataTable dt = c.getareabycityid(Convert.ToInt32(DropDownList1.SelectedValue.ToString()));
        if (dt.Rows.Count>0)
        {
            DataRow dr = dt.NewRow();
            dr["area"] = "select area ";
            dt.Rows.InsertAt(dr, 0);
            DropDownList7.DataSource = dt;
            DropDownList7.DataTextField = "area";
            DropDownList7.DataValueField = "areaid";
            DropDownList7.DataBind();

        }

    }

}

使用的方法

public DataTable getallcity()
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
    string sql = "select * from tblcity";
    SqlCommand cmd = new SqlCommand(sql, con);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    //---datasource---da----datatable,dataset

    DataTable dt = new DataTable();
    da.Fill(dt);

    return dt;
}
public DataTable getareabycityid(int cityid)
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
    string sql = "select * from tblarea where cityid=@cityid";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@cityid", cityid);
    SqlDataAdapter da = new SqlDataAdapter(cmd);

    //---datasource---da----datatable,dataset

    DataTable dt = new DataTable();
    da.Fill(dt);

    return dt;
}

2 个答案:

答案 0 :(得分:1)

首先,您的<asp:DropDownList ID="DropDownList1">应包含Autopostback属性,如下所示:

<asp:DropDownList id="ddlId" runat="server" AutoPostBack="True"/>

然后我会在

之前设置一个断点
if (dt.Rows.Count>0)
{
}

并检查此DataTable dt包含的内容

答案 1 :(得分:0)

您需要为下拉列表AutoPostBack = true

设置属性DropDownList1