惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - 小飞虫

遍历 Session,Cookie,Application JS获取URL参数值 - 小飞虫 - 博客园 PHP时间戳 Mysql 备注数据库脚本 gridview 为CommandArgument绑定行号 .net 3.5 属性简写 有效解决CSS兼容性的技巧 DropDownList绑定枚举类型 网上找的JS身份证验证,修复了一个日期验证的bug 几个js的正则表达式验证 通过javascript修改文本框内容后提交 正则表达表手机号码 修改成员管理默认的密码规则 js密码验证 js邮件地址验证脚本 asp.net 上传图片并生成缩略图 .net2.0邮件发送程序 从框架中关闭浏览器的JS脚本 关于web.config的问题
如何避免DropDownList绑定无效值时抛出异常
小飞虫 · 2010-03-17 · via 博客园 - 小飞虫

我们在修改资料时,通常会遇到把读取的原数据绑定到DropDownList的情况,由于种种原因DropDownList的项目中可能已经不包含和源数据对应该的项目。

<asp:DropDownList ID="ddl" runat="server">
        
<asp:ListItem>1</asp:ListItem>
        
<asp:ListItem>2</asp:ListItem>
        
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {

    ddl.SelectedValue = "4";

  }

}

如果是这样的话,程序就会抛出异常,因为DropDownList 中并没有4这一项

那么如何避免这种情况呢

请看下面代码

ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("4"));

这样,如果没有在列表中找到该项目,就会返回0,也就是会选中DropDownList 的第一项,而不会抛出异常