dropdownlist控件的几个属性selectedIndex、selectedItem、selectedValue、selectedItem.Text、selectedItem.value的区别

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,可读可写

光看文字可能不太理解,我也是通过程序来加深理解的,下面举个例子:.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropdown.aspx.cs" Inherits="dropdown" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Value="1">北京</asp:ListItem>
            <asp:ListItem Value="2">上海</asp:ListItem>
            <asp:ListItem Value="3">广州</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="check" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text=""></asp:Label><br />
        <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
        <br />
        <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
    
    </div>
    </form>
</body>
</html>

  后台代码

using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
  
public partial class dropdown : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {    
    }  
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        Label1.Text = "selectedIndex=" + DropDownList1.SelectedIndex;  
        Label2.Text = "selectedItem=" + DropDownList1.SelectedItem;  
        Label3.Text = "selectedValue=" + DropDownList1.SelectedValue;  
        Label4.Text = "selectedItem.text=" + DropDownList1.SelectedItem.Text;  
        Label5.Text = "selectedItem.value=" + DropDownList1.SelectedItem.Value;  
    }  
}  

  

原文

猜你喜欢

转载自www.cnblogs.com/ZkbFighting/p/9469267.html