ASP.NET MVC DropDownList資料繫結及使用詳解

來源:互聯網
上載者:User

一:DropDownList
1.1 DropDownList綁定資料
1.1.1 DropDownList 固定綁定
這種方式適合那些已經固定的資料繫結到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 動態綁定
前台:
後台:兩種方法:(注意,每次綁定都要清除一下原來的記錄,例: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("選擇資料", "隨機綁定"));//插入預設項,此舉必須放到資料繫結之後效果:

第二種: 複製代碼 代碼如下: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, "選擇網吧");
DropDownList1.Items[0].Value = "0"; 或
// DropDownList1.Items.Insert(0, new ListItem("選擇資料", "隨機綁定"));//插入預設項,此舉必須放到資料繫結之
}
else
{
DropDownList1.Items.Insert(0, "無網吧記錄");
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的選項,也就是選擇item值<asp:Listitem value="1">男</asp:Listitem> 取 男
.net 中DropDownList1.SelectedItem.ToString();
javascript document.getElementByIdx_x("DropDownList1").options[document.getElement("selectID").selectedIndex].value
三:DropDownList1事件問題
重點:使用OnTextChanged,OnSelectedIndexChanged事件時,必須設定 複製代碼 代碼如下:<asp:DropDownList runat="server" OnTextChanged="DropDownList1_TextChanged"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1">

OnTextChanged,OnSelectedIndexChanged這兩個事件具體有什麼區別,我也沒測試出來,只知道OnSelectedIndexChanged這個事件要比OnTextChanged執行的早,也就是如果這兩個事件都存在,會首先執行OnSelectedIndexChanged這個事件,然後才執行OnTextChanged.
四:如何避免DropDownList下拉框中的值重複添加
AppendDataBoundItems是否填加重複值。真為添加,假為不填加
原因:DropDownList控制項AppendDataBoundItems屬性設定為"True"了的,改為False即可。
例如:如果專業後的DropDownList控制項AppendDataBoundItems屬性設定為"True",那麼選擇院系後專業裡的值會不斷添加。
五:區別 複製代碼 代碼如下:depart_ddl.Items.Insert(0,new ListItem("不選該項","0")); 這是在首項添加資料。
Items.Add是在最後添加
DropDownList1.Items.Add(new ListItem("Text","value")); 是在最後添加
DropDownList1.Items.Insert(Index,new ListItem("Text","value"));這是在首項添加資料。

六:從資料庫中讀取資料,並綁定到DropDownList中 複製代碼 代碼如下:if (ds.Tables[0].Rows[0]["State"].ToString ()=="True")
{
DropDownListState.Items.FindByValue("1").Selected =true;
}
else
{
DropDownListState.Items.FindByValue("0").Selected =true;
}

相關文章

聯繫我們

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