• SelectedValue与SelectedItem.Value的区别


    public virtual ListItem SelectedItem {
        get {
            int selectedIndex = this.SelectedIndex;
            if (selectedIndex >= 0) {
                return this.Items[selectedIndex];
            }
            return null;
        }
    }
    public virtual string SelectedValue {
        get {
            int selectedIndex = this.SelectedIndex;
            if (selectedIndex >= 0) {
                return this.Items[selectedIndex].Value;
            }
            return string.Empty;
        }
    }

    在没有选定任何项的情况下,SelectedValue默认值是string.Empty,而SelectedItem默认值是null(也就是说通过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,可读可写

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

    前台代码:

     

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

     

    后台代码:

     

    代码
    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 }

     

    运行效果如下:

    选择北京时      选择上海时     选择广州时

  • 相关阅读:
    Redhat7.x静默安装19C客户端
    利用增量备份修复DG备库中的gap>>>>>>>>>>>有新增数据文件
    利用增量备份修复DG备库中的gap>>>>>>>>>>>无新增数据文件
    ORA-01665 control file is not a standby control file
    ORA-01110 ORA-01122 ORA-01110 ORA-01200解决办法
    Zabbix5.0+Grafana可视化部署教程
    RedHat 7.5配置bonding双网卡绑定(转)
    11.2.0.1 RAC环境部分磁盘组无法自动挂载,导致数据库实例无法启动(转)
    11.2.0.1 RAC环境经典bug CRS-4124: Oracle High Availability Services startup failed.
    Git配置SSH及常用命令
  • 原文地址:https://www.cnblogs.com/12go/p/2206916.html
Copyright © 2020-2023  润新知