日期:2014-05-16  浏览次数:20386 次

关于三级联动数据绑定问题,我用后台绑定数据,感觉不太协调,能不能优化一下?
本帖最后由 q919905205 于 2014-04-28 10:05:03 编辑
后台数据绑定

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            string id = TextBox1.Text;
            SqlParameter[] val = new SqlParameter[] { 
            new SqlParameter("@id",id)
            };
            string sql = "select color from ruku where id=@id";
            DataTable dt = DAL.DBHelper.GetDataSet(sql,val);
            //DataTable dt1 = new DataTable();
            //dt1.Columns.Add("col", typeof(System.String));
            
            List<string> list = new List<string>();
            foreach (DataRow r in dt.Rows)
            {
                string vale = r["color"].ToString();
                if (!list.Contains(vale))
                {
                    list.Add(vale);
                }
            }
            DropDownList1.DataSource = list;
            DropDownList1.DataBind();
        }


前台代码

<div>
        编号:<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        &nbsp;&nbsp;&nbsp; 颜色:<asp:DropDownList ID="DropDownList1" Width="100px" runat="server">
        </asp:DropDownList>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="查  询" Width="94px" OnClientClick="return fun()"
            OnClick="Button1_Click" />
    </div>

------解决方案--------------------
Refer here:
http://www.cnblogs.com/insus/archive/2011/07/04/2097059.html