Webform 三級聯動例子

來源:互聯網
上載者:User

標籤:

首先分別做三個下拉式清單

<body>    <form id="form1" runat="server">        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList>        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList>        <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList></form></body>

建類:China實體類

public China()    {            }    private string code;    public string Code    {        get { return code; }        set { code = value; }    }    private string name;    public string Name    {        get { return name; }        set { name = value; }    }    private string prentcode;    public string Prentcode    {        get { return prentcode; }        set { prentcode = value; }    }

資料訪問類:

public class ChinaData{    SqlConnection conn = null;    SqlCommand cmd = null;    public ChinaData()    {        conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");        cmd = conn.CreateCommand();    }    public List<China> Select(string pcode)    {        cmd.CommandText = "select * from ChinaStates where [email protected]";        cmd.Parameters.Clear();        cmd.Parameters.AddWithValue("@pcode", pcode);        conn.Open();        SqlDataReader dr = cmd.ExecuteReader();        List<China> list = new List<China>();        if (dr.HasRows)        {            while (dr.Read())            {                China data = new China();                data.Code = dr[0].ToString();                data.Name = dr[1].ToString();                data.Prentcode = dr[2].ToString();                list.Add(data);

            }


 

        }


 

        conn.Close();


 

        return list;

}


 

}

 

.cs後台代碼

首先資料繫結:

if (!IsPostBack)        {  //調用方法綁定資料         Bind(DropDownList1,new ChinaStatesData().Select("0001"));Bind(DropDownList2, new ChinaStatesData().Select(DropDownList1.SelectedValue));Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));        }//委託        DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;        DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;
//造方法:private void Bind(DropDownList ddl, List<ChinaStates> list)    {        ddl.DataSource = list;        ddl.DataTextField = "AreaName";        ddl.DataValueField = "AreaCode";        ddl.DataBind();    }
//SelectedIndexChanged事件 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)    {        Bind(DropDownList2, new ChinaStatesData().Select(DropDownList1.SelectedValue));//填充市        Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));//填充區    }void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)    {        Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));//填充區    }

代碼部分完成後,將下拉式清單的AutoPostBack屬性改為true

Webform 三級聯動例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.