有两个dropdownlist属于联动的,从数据库捞值的。
是个注册页面,问题在提交后如输入有误,dropdownlist就恢复默认的第一个值了,如何让之前选取的依然没变动。

解决方案 »

  1.   

    联动的方法很多,下面是一个完全asp.net的实现方法,直接拷贝代码即可看效果的。<%@ Page Language="C#" Debug="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
      //测试数据
      String[] A = { "A", "B", "C" };
      Hashtable b = new Hashtable();  protected void Page_Load(object sender, EventArgs e)
      {
        b.Add("A", "A1:A2");
        b.Add("B", "B1:B2");
        b.Add("C", "C1:C2");
        if (!Page.IsPostBack)
        {
          DropDownList1.DataSource = A;
          DropDownList1.DataBind();
          DropDownList2.DataSource = b["A"].ToString().Split(':');
          DropDownList2.DataBind();
        }  }  protected void Button1_Click(object sender, EventArgs e)
      {
        Response.Write(DropDownList1.SelectedValue + ":" + DropDownList2.SelectedValue);
      }  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
        DropDownList2.DataSource = b[DropDownList1.SelectedValue].ToString().Split(':');
        DropDownList2.DataBind();
      }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
      </asp:DropDownList>
      <asp:DropDownList ID="DropDownList2" runat="server">
      </asp:DropDownList>
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
      </form>
    </body>
    </html>
      

  2.   

    因为是联动,所以要根据第一个去改变第二个,所以第一个dropdownlist的autopostback肯定为true,
    那么你选择一次,他页面刷新一次,dropdownlist重新填充一次,他肯定不会停留在你第一次选择的那个value上。Page_Load事件下面加上 if (!Page.IsPostBack)
    然后在这个if下面写实现过程。
      

  3.   

    我不是求联动的方法,联动完全OK的。
    比如一个注册页面,如果说你填写的用户名重复,那肯定不能通过验证,那么这个时候两个dropdownlist的都默认选择到第一个value上了,我是求如果没通过验证让他保持之前选项不变的方法~
      

  4.   

    在验证失败后用S protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    ddlMedia.SelectedIndex = Convert.ToInt32(Session["mediaIndex"]);
                    ddlQuarter.SelectedIndex = Convert.ToInt32(Session["quarterIndex"]);
                }
            }        protected void Button1_Click(object sender, EventArgs e)
            {
                //if(验证失败)
                Session["mediaIndex"] = ddlMedia.SelectedIndex;
                Session["quarterIndex"] = ddlQuarter.SelectedIndex;
                Response.Redirect("~/WebForm1.aspx");
                        }ession存起来,然后在回发的过程中重新设置就行啦