当前位置:编程学习 > C#/ASP.NET >>

c# 两个dropdownlist,第一个正常,第二个为什么点击了选项之后框里面显示的总是第一项

具体说明请看http://zhidao.baidu.com/question/492614125.html?quesup2#
在线等啊
  哦,是为二级联动 --------------------编程问答-------------------- 去掉第二个dropdownlist的autopostback撒,你回发后第二个下拉框重新绑定了,自然又回到第一个去了 --------------------编程问答-------------------- 去掉了啦,可是结果还是一样的,并且第二个dropdownlist的DropDownListMajor_SelectedIndexChanged事件无法触发.这又是为什么? --------------------编程问答--------------------

        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true">
        </asp:DropDownList>


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //定义数据表
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Class", typeof(string)));

            for (int i = 1; i < 7; i++)
            {
                DataRow drr = dt.NewRow();

                drr["ID"] =  i;
                drr["Class"] = i.ToString() + "年级";

                dt.Rows.Add(drr);
            }
            //绑定下拉
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "Class";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("", "-1"));
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //这里你根据自己的实际情况判断后绑定
        if (DropDownList1.SelectedItem.Value == "1")
        {
            DropDownList2.Items.Add(new ListItem("学生1", "1"));
            DropDownList2.Items.Add(new ListItem("学生2", "2"));
            DropDownList2.Items.Insert(0, new ListItem("", "-1"));
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string MM=DropDownList1.SelectedItem.Text.ToString()+DropDownList2.SelectedItem.Text.ToString();
        Response.Write(MM);//输出两个下拉框的选择项
    }
--------------------编程问答-------------------- Refer:
http://www.cnblogs.com/insus/archive/2011/07/04/2097059.html
http://www.cnblogs.com/insus/archive/2012/10/16/2725307.html --------------------编程问答-------------------- --------------------编程问答-------------------- 用ajax都好  还省得去刷新 --------------------编程问答--------------------
DDL.Items.FindByValue("").seleced=true  是用 FindByValue这个就好了 --------------------编程问答--------------------

        <asp:DropDownList Width="100" ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="ID" AutoPostBack="True"></asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [aa]">
        </asp:SqlDataSource>
        <asp:DropDownList Width="100" ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Sex" DataValueField="ID" AutoPostBack="True"></asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [cc] WHERE ([ID] = @ID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" DefaultValue="1" Name="ID" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
--------------------编程问答-------------------- 关键要加:AutoPostBack="True" --------------------编程问答-------------------- 一般都是数据错了,值相同了 --------------------编程问答-------------------- AutoPostBack="True" 这时候才能触发你的DropDownListMajor_SelectedIndexChanged事件

而AutoPostBack="True" 的话优惠影响你的绑定

所以你需要把你的绑定放到
if(!IsPostBack)
{

}
这样第一次调用的时候绑定数据,post back的时候用你选中的那个(ViewState记住了你上次的选择,在页面load后会为你赋值) --------------------编程问答--------------------
引用 11 楼  的回复:
AutoPostBack="True" 这时候才能触发你的DropDownListMajor_SelectedIndexChanged事件

而AutoPostBack="True" 的话优惠影响你的绑定

所以你需要把你的绑定放到
if(!IsPostBack)
{

}
这样第一次调用的时候绑定数据,post back的时候用你选中的那个(ViewState记住了你上次的选……


同意
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,