记录三个DropDownList的实践操作

1.

先上效果图

多选框选择其中一个选项,多行文本框输出这个选项的基本数据,以及下面的两个按钮的功能。

前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack = "true"><asp:ListItem Value="A">first</asp:ListItem><asp:ListItem Value="B">second</asp:ListItem><asp:ListItem Value="C">Third</asp:ListItem></asp:DropDownList><br /><br /><br /><asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="120px" Width="208px"></asp:TextBox><br /><br /><asp:Button ID="Button1" runat="server" Text="自动选择第三项" onclick="Button1_Click" Width="128px" />&nbsp;<asp:Button ID="Button2" runat="server" Text="清除所有项" onclick="Button2_Click" Width="121px" /></div></form>
</body>
</html>

后端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){TextBox1.Text = "索引值:" + DropDownList1.SelectedIndex;TextBox1.Text += "\nText值:" + DropDownList1.SelectedItem.Text;TextBox1.Text += "\nValue值:" + DropDownList1.SelectedValue;TextBox1.Text += "\n总共多少项:" + DropDownList1.Items.Count;}protected void Button1_Click(object sender, EventArgs e){DropDownList1.SelectedItem.Selected = false; //这时候选中状态的选中项要先取消DropDownList1.Items[2].Selected = true;TextBox1.Text = "索引值:" + DropDownList1.SelectedIndex;TextBox1.Text += "\nText值:" + DropDownList1.SelectedItem.Text;TextBox1.Text += "\nValue值:" + DropDownList1.SelectedValue;TextBox1.Text += "\n总共多少项:" + DropDownList1.Items.Count;}protected void Button2_Click(object sender, EventArgs e){DropDownList1.Items.Clear();}
}

DropDownList1_SelectedIndexChanged()方法的快捷创建方式:直接在拆分交互界面双击这个多选框

2.

先上效果图

看图片的信息基本上就可以知道
①多选框选中一个选项,跳转按钮实现跳转页面;
②修改选项;
③增加选项;
④在Page_Load()方法绑定数据源以及创建选项

前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!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" DataSourceID="AccessDataSource1" DataTextField="网站名称" DataValueField="网址"></asp:DropDownList>&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="跳转" /><asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/test.mdb" SelectCommand="SELECT [网站名称], [网址] FROM [TB_HLINKS]"></asp:AccessDataSource><br /><br /><asp:Button ID="Button2" runat="server" Text="把第三项设置为拼多多" onclick="Button2_Click" Width="200px" />
&nbsp;<br /><br /><asp:Button ID="Button3" runat="server" Text="增加一项" onclick="Button3_Click" /><br /><br /><asp:Panel ID="Panel1" runat="server"></asp:Panel></div></form>
</body>
</html>

添加数据源的操作我就不细说了,可以参考我上一篇博客:
asp.net_css应用

后端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;public partial class Default2 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){//绑定数据源ArrayList arr = new ArrayList();arr.Add("a");arr.Add("b");arr.Add("c");DropDownList dl = new DropDownList(); //生成控件dl.DataSource = arr;dl.DataBind();Panel1.Controls.Add(dl);}protected void Button1_Click(object sender, EventArgs e){Response.Redirect(DropDownList1.SelectedValue); //进行跳转}protected void Button2_Click(object sender, EventArgs e){DropDownList1.Items[2].Text = "拼多多";DropDownList1.Items[2].Value = "https://www.pinduoduo.com/";}protected void Button3_Click(object sender, EventArgs e){ListItem item = new ListItem();item.Text = "唯品会";item.Value = "https://www.vip.com/";DropDownList1.Items.Add(item);}
}

修改选项用到了DropDownList.Items[i].Text和.Value

添加选项是new一个ListItem对象来进行操作。

Page_load()当中这一句,等价于从工具箱直接添加控件。

DropDownList dl = new DropDownList(); //生成控件

而这两句,等价于在拆分交互页面直接点击编辑选择项

dl.DataSource = arr;
dl.DataBind();

3.实现一个登录界面,用户需要从两个相互关联的下拉列表框中选择用户所在城市

先上效果图:


看图可知,选择了一个省份,会自动生成省内的所有市(自己编辑)。

前端代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %><!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:TextBox ID="TextBox1" runat="server"  AutoPostBack = "true"></asp:TextBox>&nbsp;<asp:Label ID="NameLabel" runat="server" Text="" ForeColor = "red"></asp:Label><br /><br />密码:<asp:TextBox ID="TextBox2" runat="server"  AutoPostBack = "true"></asp:TextBox>&nbsp;<asp:Label ID="PasswordLabel" runat="server" Text="" ForeColor="red"></asp:Label><br /><br />所在城市:<asp:DropDownList ID="dl_province" runat="server" AutoPostBack = "true" onselectedindexchanged="dl_province_SelectedIndexChanged" style="height: 23px"><asp:ListItem>福建省</asp:ListItem><asp:ListItem>广东省</asp:ListItem></asp:DropDownList>&nbsp;<asp:DropDownList ID="dl_city" runat="server"></asp:DropDownList>&nbsp;<asp:Label ID="L" runat="server" Text="" ForeColor="red"></asp:Label><br /><br />
&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" Text="登录" onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;<asp:Button ID="Button2" runat="server" Text="重置" onclick="Button2_Click" /><br /><br /><br /><asp:Label ID="Label1" runat="server" Text="" ></asp:Label></div></form>
</body>
</html>

后端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class Default3 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void dl_province_SelectedIndexChanged(object sender, EventArgs e){if (dl_province.SelectedItem.Text == "福建省"){string[] str = {"福州市","厦门市","漳州市"};dl_city.Items.Clear();dl_city.DataSource = str;dl_city.DataBind();}if (dl_province.SelectedItem.Text == "广东省"){dl_city.Items.Clear();string[] str = { "中山市", "珠海市", "深圳市" };dl_city.DataSource = str;dl_city.DataBind();}}protected void Button1_Click(object sender, EventArgs e){if (TextBox1.Text != "" && TextBox2.Text == "123456"){Label1.Text = TextBox1.Text + "居住在" + dl_province.Text + dl_city.Text;}else if(TextBox1.Text == ""){NameLabel.Text = "姓名不能为空";}else if(TextBox2.Text != "123456"){PasswordLabel.Text = "密码不正确";}}protected void Button2_Click(object sender, EventArgs e){TextBox1.Text = "";TextBox2.Text = "";Label1.Text = "";dl_province.SelectedItem.Selected = false;dl_city.SelectedItem.Selected = false;dl_city.Items.Clear();NameLabel.Text = "";PasswordLabel.Text = "";}
}

asp.net_DropDownList应用相关推荐

  1. ASP.NET MVC 2示例Tailspin Travel

    Tailspin Travel 是一个旅游预订的应用程序示例,最新版本采用ASP.NET MVC 2技术构建,主要使用 DataAnnotations 验证, 客户端验证和ViewModels,还展示 ...

  2. 通过jQuery调用ASP.NET的AJAX

    传统上,在ASP.NET页面中实现AJAX的方法是拖放一个ScriptManager控件,并进行一系列的设置和相关编程.但事实上,这并不是理想的解决方案.使用jQuery来调用ASP.NET后台的方法 ...

  3. Asp.Net Core在线生成二维码

    前言: 原先用zxing Code写过基于Winfrom的批量生成二维码工具,以及单个生成二维码工具:批量生成二维码Gihub源代码 今天尝试用QRCoder 加 Asp.Net Core 写了一个在 ...

  4. Asp.net MVC中的ViewData与ViewBag

    在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...

  5. ASP.NET MVC Identity 兩個多個連接字符串問題解決一例

    按照ASP.NET MVC Identity建立了一個用戶權限管理模塊,由于還要加自己已有的數據庫,所以建立了一個實體模型,建立了之后,發現登錄不了: 一直顯示"Login in faile ...

  6. ASP.NET页面之间传值的方式之QueryString(个人整理)

    QueryString Querystring也叫查询字符串,这种页面间传递数据是利用网页地址URL.如果要从A页面跳转到B页面,则可以用Request.Redirect("B.aspx?参 ...

  7. ASP.NET MVC 4 (十三) 基于表单的身份验证

    在前面的章节中我们知道可以在MVC应用程序中使用[Authorize]特性来限制用户对某些网址(控制器/控制器方法)的访问,但这都是在对用户认证之后,而用户的认证则依然是使用ASP.NET平台的认证机 ...

  8. [Asp.net 5] Options-配置文件(2)

    很久之前写过一篇介绍Options的文章,2016年再打开发现很多变化.增加了新类,增加OptionMonitor相关的类.今天就对于这个现在所谓的新版本进行介绍. 老版本的传送门([Asp.net ...

  9. asp.net mvc 学习

    Routing讲解: http://www.cnblogs.com/wangiqngpei557/p/3379095.html Filter讲解: http://www.cnblogs.com/ymn ...

最新文章

  1. AndroidManifest.xml文件详解(activity)(三)四种工作模式
  2. python爬虫案例-Python爬虫案例集合
  3. MATLAB“figure”使用详解
  4. java异步框架feed,Java:IO流里面的BuffeedReader
  5. CDH6.3.2添加Hue服务时,验证数据库连接报错 Unexpected error. Unable to verify database connection.
  6. 3D人脸重建——PRNet网络输出的理解
  7. 作者:郭旦怀(1973-),男,博士,中国科学院计算机网络信息中心副研究员、硕士生导师。...
  8. c#listbox使用详解和常见问题解决
  9. C# HashSet 实例
  10. java迷宫算法继承_求Java关于迷宫的算法(用栈实现)
  11. 在线解析下载微软官方商店离线安装包
  12. bitvise ssh client 连接linux,secureCRT + Bitvise SSH Client实现ssh隧道远程
  13. 从山寨机看手机的未来
  14. Delphi第三方组件--Delphi第三方控件大比拼
  15. html 颜色渐变动画效果,js实现按钮颜色渐变动画效果
  16. 全流程东方时尚C1考试经历
  17. 推荐使用的热电阻Pt100测温电路
  18. 检验方法的验证、确认步骤及详细计算方法
  19. 【R语言文本挖掘】:n-grams和相关性计算
  20. Python判断字符串是否以字母开头

热门文章

  1. 2018年工作第一次从深圳回河南老家(下)
  2. 计算机专业必须要读的专业书推荐
  3. 魔百盒m401a折腾Armbian+qinglong -- 1
  4. Java项目:JSP民宿预订网站信息管理平台
  5. unity(Koreographer实现官网的案例)
  6. openeuler22.03实时系统安装及部署
  7. unity期末作业-拼图游戏
  8. 高职单招数学公式大全,高职单招数学重点公式
  9. 向前logistic回归与向后筛选出一样的变量_风控建模之特征筛选与建模(python)...
  10. Python练习题答案: 摩门经【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战