asp.net DropDownList無重新整理ajax二級聯動實現詳細過程

來源:互聯網
上載者:User

資料庫實現,添加兩表表1,pingpai,表2,type,具體資料庫實現看自己的理解:

頁面主要代碼:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Width="200" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" Width="200">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>

//以上代碼實現ajax的無重新整理效果

程式主要代碼:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDrop();
}
}
private void BindDrop()
{
//將資料捆綁到下拉式清單中
string sqlStr = "select * from pingpai";
DataTable dt=DataBase.GetTable(sqlStr);
DropDownList1.DataTextField = "pingpai"; //設定列表顯示的字
DropDownList1.DataValueField = "typeid"; //設定列表提交後獲得的欄位,自己理解為隱藏綁定資料
DropDownList1.DataSource = dt.DefaultView;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("請選擇車子品牌", ""));//第一項中加入內容,重點是綁定後添加
DropDownList2.Items.Insert(0, new ListItem("請選擇車子品牌型號", ""));//第一項中加入內容,重點是綁定後添加
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int typeid = Convert.ToInt32(DropDownList1.SelectedValue);//頁面載入後DropDownList1.DataValueField隱藏繫結資料,後邊根據它查詢DropDownList2要顯現的資料
string sqlStr = "select * from type where typeid='" + typeid + "'";

DataTable dt = DataBase.GetTable(sqlStr);
DropDownList2.DataTextField = "type"; //設定DropDownList1事件SelectedIndexChanged改變後DropDownList2列表顯示的資料
DropDownList2.DataSource = dt.DefaultView;
DropDownList2.DataBind();;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.