[Asp.net]DropDownList改变默认选中项的两种方式
引言
其实是不想总结这方面的内容,发现太简单了,可是在这上面也栽了跟头。所以还是记录一下吧,算是提醒自己,不要太看不起太基础的东西,有这种心理,是会载大跟头的。
一个例子
这里模拟一下最常用的一个例子,在列表中,选择修改,将选中的记录,在上面显示,并改变DropDownList中的默认选中项。
方式一
代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Wolfy.DropDownListDemo.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
编号:<asp:Literal Text="" runat="server" ID="LiteralID" /><br />
省市:<asp:DropDownList ID="DropDownListProvince" runat="server"></asp:DropDownList><br />
级别:<asp:Literal Text="" runat="server" ID="LiteralLevel" /><br /> </div>
<asp:Repeater ID="RepeaterList" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>编号</th>
<th>省</th>
<th>级别</th>
<th>操作</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("ID") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Pid") %></td>
<td>
<asp:LinkButton Text="修改" runat="server" ID="LinkUpdate" OnClick="LinkUpdate_Click" CommandArgument='<%#Eval("ID") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater> </form>
</body>
</html>
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace Wolfy.DropDownListDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownListProvince.DataSource = GetList();
DropDownListProvince.DataTextField = "Name";
DropDownListProvince.DataValueField = "ID";
DropDownListProvince.DataBind();
RepeaterList.DataSource = GetList();
RepeaterList.DataBind();
}
protected List<Province> GetList()
{
List<Province> list = new List<Province>();
list.Add(new Province() { ID = , Name = "北京", Pid = });
list.Add(new Province() { ID = , Name = "上海", Pid = });
list.Add(new Province() { ID = , Name = "河南", Pid = });
list.Add(new Province() { ID = , Name = "河北", Pid = });
list.Add(new Province() { ID = , Name = "湖南", Pid = });
list.Add(new Province() { ID = , Name = "湖北", Pid = });
return list;
} protected void LinkUpdate_Click(object sender, EventArgs e)
{
LinkButton link = sender as LinkButton;
if (link != null)
{
int id = Convert.ToInt32(link.CommandArgument);
Province pro = GetList().Find(a => a.ID == id);
this.LiteralID.Text = pro.ID.ToString();
this.LiteralLevel.Text = pro.Pid.ToString();
//DropDownList的index是从零开始的 而绑定的数据的ID是从1开始的,所以为了对应需减一
//改变默认选中项
this.DropDownListProvince.SelectedIndex = pro.ID-;
}
}
}
}
方式二
protected void LinkUpdate_Click(object sender, EventArgs e)
{
LinkButton link = sender as LinkButton;
if (link != null)
{
int id = Convert.ToInt32(link.CommandArgument);
Province pro = GetList().Find(a => a.ID == id);
this.LiteralID.Text = pro.ID.ToString();
this.LiteralLevel.Text = pro.Pid.ToString();
//方式二
ListItem item = DropDownListProvince.Items.FindByText(pro.Name);
if (item != null)
{
//防止出现多选的情况,将选中项 清除
DropDownListProvince.ClearSelection();
item.Selected = true;
}
}
}
当时lz载跟头的地方就是报DropDownList出现多选的bug,很无语,这里没办法还原那个bug的场景了,就这样记录一下吧。在报出现那个错误时,将选中项清除就可以了。
总结
工作中,遇到的一个小bug,总结一下,提醒自己以后不要再犯这样的低级错误,无法饶恕。
Demo下载:http://pan.baidu.com/s/1sjJcutf
[Asp.net]DropDownList改变默认选中项的两种方式的更多相关文章
- .Net默认IE版本号的两种方式
1.直接在页面的header部位meta标签中加入如下代码 <meta http-equiv="X-UA-Compatible" content="IE=8&quo ...
- Asp.net Web API 返回Json对象的两种方式
这两种方式都是以HttpResponseMessage的形式返回, 方式一:以字符串的形式 var content = new StringContent("{\"FileName ...
- eclipse 使用tomcat运行JavaWeb项目,文件修改后为何不用重启tomcat? (运行web项目的4种方式)探究
1.情景说明 在eclipse中,为什么Java文件修改后,重启tomcat class文件才能生效? 为什么jsp修改后,不需重启tomcat就能立即生效? 为什么静 ...
- ASP.NET Web API接受AngualrJS的QueryString的两种方式
ASP.NET Web API如何接受来自AngualrJS的QueryString呢?本篇体验两种方式. 第一种方式:http://localhost:49705/api/products?sear ...
- angularjs通过ng-change和watch两种方式实现对表单输入改变的监控
angularjs通过ng-change和watch两种方式实现对表单输入改变的监控 直接上练习代码 <!DOCTYPE html> <html xmlns="http:/ ...
- TOMCAT下面发布项目的4种方式
摘要 TOMCAT下面发布项目的4种方式,可用于在平时资料查询. 第一种方法: 将web项目文件件拷贝到webapps 目录中:或者直接通过Eclipse发布到Tomcat上. 第二种方法: 在tom ...
- linux 设置开机启动项两种方式
原文链接:http://blog.csdn.net/karchar/article/details/52489572 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务. 在解问题之前 ...
- win10应用程序添加到开机启动项的两种解决办法
原文 win10应用程序添加到开机启动项的两种解决办法 在windows10系统中,如果想让应用程序在开机之后自动运行起来,可以怎么做呢? 方法一: 1.首先创建应用程序的快捷方式 找到自己想加入开机 ...
- struts2设置<s:select>默认选中项的方法
struts2的select标签中,常用的有以下几个属性:(1)struts2中的select 标签中,必须设置的属性只有一个,即是list.(2)select标签的list中必须有值,不然会报错.如 ...
随机推荐
- BeanUtils简化数据封装
BeanUtils主要用来封装JavaBean的. 1.什么是JavaBean JavaBean指的是标准的类. 要求: 1. 类必须被public修饰2. 必须提供空参的构造器3. 成员变量必须使用 ...
- 一键去除网页BOM属性【解决乱码,头部空白,问题】
几个常出现的问题: 1.网站打开空白 2.页面头部出现多余的空白 3.网站出现乱码,如“锘�” 解决方法可以是: 1.选用专业的编辑器,例如notepad++,sublime,editplus这样不会 ...
- Eolinker——代码注入插入随机参数值
因为目前eolinker的API自动化测试不支持“构造参数”,所以用到随机数时,可使用代码注入的方式来实现 分步指南 示例:“重置密码”接口,每次运行重置的密码要求不重复 再此接口的“代码注入”区域写 ...
- 木块问题(UVa101)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- 【PAT】1002. 写出这个数 (20)
1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式 ...
- Templated Helper Methods(二)
1.Label and Display Elements 2.Whole-Model Templated Helpers 3.Using Metadata to Control Edi ...
- C# asp.net 实现导出Excel
原文地址:传送门 这段时间用到了导出Excel的功能,这个功能还是比较常用的,我常用的有两个方法,现在整理一下,方便以后查看. 1.实现DataTable数据导出到本地,需要自己传进去导出的路径. / ...
- 二十二 使用__slots__
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: class Student(object): pa ...
- Python 函数系列 - Str对path的处理
由此可见,“\”是转义字符,它能够将第2个“\”从转义字符转回普通字符,从而“\n”就不再起到换行符的作用. 这样操作虽然简单,但是遇到下方这个路径,看起来就会有些麻烦! path = 'D:\new ...
- java -jar demo.jar
部署springboot项目 生成jar包其实还是依赖springboot的jar才能跑起来,为什么呢? 1.在C盘手工创建了一个文件夹,是拷贝了demo.jar这个jar包运行是报错的. 2.在D: ...