none
DropDownList三级联动为什么最后一个DropDownList得不到值(或者没有变化) RRS feed

  • 问题

  • A.aspx页面代码如下:
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                                字段一:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                                    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                                </asp:DropDownList>
                                字段二:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
                                    onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                                </asp:DropDownList>
                                字段三:<asp:DropDownList ID="DropDownList3" runat="server">
                                </asp:DropDownList>
                            </ContentTemplate>
                        </asp:UpdatePanel>

    注意:"字段三"是日期类型

    A.aspx.cs代码如下:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("select  字段一 from 表1", new SqlConnection(System.Configuration.ConfigurationManager
                            .ConnectionStrings["ConnectionString1"].ConnectionString)))
                {
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataTextField = "字段一";
                    DropDownList1.DataValueField = "字段一";
                    DropDownList1.DataBind();
                }
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("select 字段二 from 表1 where 字段二='" + DropDownList1.SelectedValue.ToString() + "'", new SqlConnection(System.Configuration.ConfigurationManager
                .ConnectionStrings["ConnectionString1"].ConnectionString)))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList2.DataSource = null;
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "字段二";
                DropDownList2.DataValueField = "字段二";
                DropDownList2.DataBind();
            }
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("select 字段三 from 表1 where 字段三='" + DropDownList2.SelectedValue.ToString() + "'", new SqlConnection(System.Configuration.ConfigurationManager
                .ConnectionStrings["ConnectionString1"].ConnectionString)))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList3.DataSource = null;
                DropDownList3.DataSource = dt;
                DropDownList3.DataTextField = "字段三";
                DropDownList3.DataValueField = "字段三";
                DropDownList3.DataBind();
            }
        }

    为什么DropDownList1变化的时候,DropDownList2有效,但DropDownList2变化的时候对DropDownList3却一直无效."字段三"是日期类型,不能直接赋值给DropDownList3??谢谢



    • 已编辑 阿波 2011年9月26日 8:39
    2011年9月26日 7:18

答案

  • 我这样看下来,感觉字段1~3都是日期类型?

    请试试看

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("select  Convert(varchar,字段一,111) As 字段一 from 表1", new SqlConnection(System.Configuration.ConfigurationManager
                            .ConnectionStrings["ConnectionString1"].ConnectionString)))
                {
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataTextField = "字段一";
                    DropDownList1.DataValueField = "字段一";
                    DropDownList1.DataBind();
                }
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("select Convert(varchar,字段二,111) As 字段二 from 表1 where Convert(varchar,字段二,111)='" + DropDownList1.SelectedValue + "'", new SqlConnection(System.Configuration.ConfigurationManager
                .ConnectionStrings["ConnectionString1"].ConnectionString)))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList2.DataSource = null;
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "字段二";
                DropDownList2.DataValueField = "字段二";
                DropDownList2.DataBind();
            }
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("select Convert(varchar,字段三,111) As 字段三 from 表1 where Convert(varchar,字段三,111)='" + DropDownList2.SelectedValue + "'", new SqlConnection(System.Configuration.ConfigurationManager
                .ConnectionStrings["ConnectionString1"].ConnectionString)))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList3.DataSource = null;
                DropDownList3.DataSource = dt;
                DropDownList3.DataTextField = "字段三";
                DropDownList3.DataValueField = "字段三";
                DropDownList3.DataBind();
            }
        }
    
    
    

     


    Shadowと愉快なコード達
    • 已标记为答案 阿波 2011年9月30日 6:22
    2011年9月26日 9:02