ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
一:DropDownList
1.1 DropDownList綁定數(shù)據(jù)
1.1.1 DropDownList 固定綁定
這種方式適合那些已經(jīng)固定的數(shù)據(jù)綁定到DropDownList上。
例
<asp:DropDownList runat="server" ID="ddlArea" Width="120px" >
<asp:Listitem value="0">選擇性別</asp:Listitem>
<asp:Listitem value="1">男</asp:Listitem>
<asp:Listitem value="2">女</asp:Listitem>
</asp:DropDownList>
1.1.2 DropDownList 動(dòng)態(tài)綁定
前臺:
后臺:兩種方法:(注意,每次綁定都要清除一下原來的記錄,例:ddlArea.Items.Clear();)
第一種:
SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs");
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn);
DataTable dt = new DataTable();
dap.Fill(dt);
DropDownList1.Items.Clear();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "job_desc";
DropDownList1.DataValueField = "job_id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("選擇數(shù)據(jù)", "隨機(jī)綁定"));//插入默認(rèn)項(xiàng),此舉必須放到數(shù)據(jù)綁定之后效果:
第二種:
SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs");
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn);
DataTable dt = new DataTable();
dap.Fill(dt);
if (dt.Rows.Count != 0)
{
DropDownList1.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList1.Items.Add(new ListItem(dt.Rows[i]["顯示值"].ToString(), dt.Rows[i]["usbkey"].ToString()));
}
DropDownList1.Items.Insert(0, "選擇網(wǎng)吧");
DropDownList1.Items[0].Value = "0"; 或
// DropDownList1.Items.Insert(0, new ListItem("選擇數(shù)據(jù)", "隨機(jī)綁定"));//插入默認(rèn)項(xiàng),此舉必須放到數(shù)據(jù)綁定之
}
else
{
DropDownList1.Items.Insert(0, "無網(wǎng)吧記錄");
DropDownList1.Items[0].Value = "0";
}
二:DropDownList1的取值問題:
2.1 取DropDownList1的索引值,也就是選擇 value 值<asp:Listitem value="1">男</asp:Listitem> 取1
.net中 DropDownList1.SelectedValue.ToString()
javascirpt var ddl1=document.getElementByIdx_x("DropDownList1").selectedIndex;
2.2 取DropDownList1的選項(xiàng),也就是選擇item值<asp:Listitem value="1">男</asp:Listitem> 取 男
.net 中DropDownList1.SelectedItem.ToString();
javascript document.getElementByIdx_x("DropDownList1").options[document.getElement("selectID").selectedIndex].value
三:DropDownList1事件問題:
重點(diǎn):使用OnTextChanged,OnSelectedIndexChanged事件時(shí),必須設(shè)置
<asp:DropDownList runat="server" OnTextChanged="DropDownList1_TextChanged"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1">
OnTextChanged,OnSelectedIndexChanged這兩個(gè)事件具體有什么區(qū)別,我也沒測試出來,只知道OnSelectedIndexChanged這個(gè)事件要比OnTextChanged執(zhí)行的早,也就是如果這兩個(gè)事件都存在,會(huì)首先執(zhí)行OnSelectedIndexChanged這個(gè)事件,然后才執(zhí)行OnTextChanged.
四:如何避免DropDownList下拉框中的值重復(fù)添加?
AppendDataBoundItems是否填加重復(fù)值。真為添加,假為不填加
原因:DropDownList控件AppendDataBoundItems屬性設(shè)置為"True"了的,改為False即可。
例如:如果專業(yè)后的DropDownList控件AppendDataBoundItems屬性設(shè)置為"True",那么選擇院系后專業(yè)里的值會(huì)不斷添加。
五:區(qū)別
depart_ddl.Items.Insert(0,new ListItem("不選該項(xiàng)","0")); 這是在首項(xiàng)添加數(shù)據(jù)。
Items.Add是在最后添加
DropDownList1.Items.Add(new ListItem("Text","value")); 是在最后添加
DropDownList1.Items.Insert(Index,new ListItem("Text","value"));這是在首項(xiàng)添加數(shù)據(jù)。
六:從數(shù)據(jù)庫中讀取數(shù)據(jù),并綁定到DropDownList中
if (ds.Tables[0].Rows[0]["State"].ToString ()=="True")
{
DropDownListState.Items.FindByValue("1").Selected =true;
}
else
{
DropDownListState.Items.FindByValue("0").Selected =true;
}
- asp.net mvc 從數(shù)據(jù)庫中讀取圖片的實(shí)現(xiàn)代碼
- asp.net mvc4 mysql制作簡單分頁組件(部分視圖)
- 利用ASP.NET MVC+EasyUI+SqlServer搭建企業(yè)開發(fā)框架
- 使用jQuery向asp.net Mvc傳遞復(fù)雜json數(shù)據(jù)-ModelBinder篇
- ASP.NET中MVC從后臺控制器傳遞數(shù)據(jù)到前臺視圖的方式
- Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼
- ASP.NET MVC 數(shù)據(jù)驗(yàn)證及相關(guān)內(nèi)容
- ASP.NET Mvc開發(fā)之刪除修改數(shù)據(jù)
- ASP.NET中MVC傳遞數(shù)據(jù)的幾種形式總結(jié)
- ASP.NET Mvc開發(fā)之查詢數(shù)據(jù)
- JQuery對ASP.NET MVC數(shù)據(jù)進(jìn)行更新刪除
- asp.net實(shí)現(xiàn)的MVC跨數(shù)據(jù)庫多表聯(lián)合動(dòng)態(tài)條件查詢功能示例
- ASP.NET MVC使用EPPlus,導(dǎo)出數(shù)據(jù)到Excel中
相關(guān)文章
asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法
這篇文章主要介紹了asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法,實(shí)現(xiàn)讀取excel數(shù)據(jù)并導(dǎo)入到SQL Server數(shù)據(jù)庫的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2015-01-01
asp.net Linq把數(shù)據(jù)導(dǎo)出到Excel的代碼
最近有需要通過WEB把數(shù)據(jù)導(dǎo)出到Excel的功能, 關(guān)于導(dǎo)出數(shù)據(jù)到Excel并無什么新奇可言,網(wǎng)絡(luò)上到處都是,但基本上都是一種模式,通過DataGrid 把數(shù)據(jù)導(dǎo)出到Excel的方式。2008-10-10
微信JS-SDK分享功能的.Net實(shí)現(xiàn)代碼
這篇文章主要介紹了微信JS-SDK分享功能的.Net實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-09-09
asp.net Gridview數(shù)據(jù)列中實(shí)現(xiàn)鼠標(biāo)懸浮變色
Gridview一般朋友們都比較常用,因?yàn)樗梢苑奖憧旖莸膶?shí)現(xiàn)我們所需的很多功能,代碼也比較簡潔。平時(shí)的項(xiàng)目中這個(gè)控件我也比較常用,其中有個(gè)功能用到的頻率也比較多。所以記錄下備忘。2010-06-06
asp.net實(shí)現(xiàn)圖片以二進(jìn)制流輸出的兩種方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)圖片以二進(jìn)制流輸出的兩種方法,以簡單實(shí)例形式分析了asp.net實(shí)現(xiàn)以二進(jìn)制流形式讀寫圖片文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
C#精髓 GridView72大絕技 學(xué)習(xí)gridview的朋友必看
C#精髓 GridView72大絕技 學(xué)習(xí)gridview的朋友可以參考下。2009-10-10
asp.net不同頁面間數(shù)據(jù)傳遞的多種方法
這篇文章主要介紹了asp.net不同頁面間數(shù)據(jù)傳遞的多種方法,包括使用QueryString顯式傳遞、頁面對象的屬性、cookie、Cache等9種方法2014-01-01

