ASP.NET二级联动菜单的实现

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

       

    }
    public void cate_bind()
    {
        adosql addproado = new adosql();
        string sqlstr = "select category,categoryid from category";
        DataSet test = addproado.todateset(sqlstr);
        category.DataSource = test;
        category.DataTextField = "category";
        category.DataValueField = "categoryid";
        category.DataBind();

    }
    public void category_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text =category.SelectedValue.ToString();

        adosql cate_chang = new adosql();
        string str_chang = "select sortsid,sortname from sorts where categoryid=" + category.SelectedValue.ToString();
        sorts.DataSource = cate_chang.todateset(str_chang);
        sorts.DataTextField = "sortname";
        sorts.DataValueField = "sortsid";
        sorts.DataBind();
    }
}
注意的是要在页面中将droplist控件添加属性autopostback=true

你可能感兴趣的:(asp.net)