dropdownlist控件的幾個屬性selectedIndex、selectedItem、selectedValue、selectedItem.Text、selectedItem.value的區別


轉自http://blog.csdn.net/iqv520/article/details/4419186

1. selectedIndex——指的是dropdownlist中選項的索引,為int,從0開始,可讀可寫

2. selectedItem——指的是選中的dropdownlist中選項,為ListItem,只讀不寫

3. selectedValue——指的是選中的dropdownlist中選項的值,為string, 只讀不寫

4. selectedItem.Text——指的是選中的dropdownlist中選項的文本內容,與selectedItems的值一樣為string,可讀可寫

5. selectedItem.value——指的是選中的dropdownlist中選項的值,與selectedValue的值一樣,為string,可讀可寫

光看文字可能不太理解,我也是通過程序來加深理解的,下面舉個例子:

前台代碼:

 

[xhtml]  view plain copy
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropdown.aspx.cs" Inherits="dropdown" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>無標題頁</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:DropDownList ID="DropDownList1" runat="server">  
  13.             <asp:ListItem Value="1">北京</asp:ListItem>  
  14.             <asp:ListItem Value="2">上海</asp:ListItem>  
  15.             <asp:ListItem Value="3">廣州</asp:ListItem>  
  16.         </asp:DropDownList>  
  17.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="check" /><br />  
  18.         <asp:Label ID="Label1" runat="server" Text=""></asp:Label>  
  19.         <br />  
  20.         <asp:Label ID="Label2" runat="server" Text=""></asp:Label>  
  21.         <br />  
  22.         <asp:Label ID="Label3" runat="server" Text=""></asp:Label><br />  
  23.         <asp:Label ID="Label4" runat="server" Text=""></asp:Label>  
  24.         <br />  
  25.         <asp:Label ID="Label5" runat="server" Text=""></asp:Label>  
  26.       
  27.     </div>  
  28.     </form>  
  29. </body>  
  30. </html>  

 

后台代碼:

 

[c-sharp]  view plain copy
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11.   
  12. public partial class dropdown : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.   
  17.     }  
  18.     protected void Button1_Click(object sender, EventArgs e)  
  19.     {  
  20.         Label1.Text = "selectedIndex=" + DropDownList1.SelectedIndex;  
  21.         Label2.Text = "selectedItem=" + DropDownList1.SelectedItem;  
  22.         Label3.Text = "selectedValue=" + DropDownList1.SelectedValue;  
  23.         Label4.Text = "selectedItem.text=" + DropDownList1.SelectedItem.Text;  
  24.         Label5.Text = "selectedItem.value=" + DropDownList1.SelectedItem.Value;  
  25.     }  
  26. }  

 運行效果如下:

    

本文轉自http://blog.csdn.net/iqv520/article/details/4419186

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM