[Asp.net]DropDownList改变默认选中项的两种方式

引言

其实是不想总结这方面的内容,发现太简单了,可是在这上面也栽了跟头。所以还是记录一下吧,算是提醒自己,不要太看不起太基础的东西,有这种心理,是会载大跟头的。

一个例子

这里模拟一下最常用的一个例子,在列表中,选择修改,将选中的记录,在上面显示,并改变DropDownList中的默认选中项。

方式一

代码:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Wolfy.DropDownListDemo.Default" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         编号:<asp:Literal Text="" runat="server" ID="LiteralID" /><br />
14         省市:<asp:DropDownList ID="DropDownListProvince"  runat="server"></asp:DropDownList><br />
15         级别:<asp:Literal Text="" runat="server" ID="LiteralLevel" /><br />
16 
17 
18     </div>
19         <asp:Repeater ID="RepeaterList" runat="server">
20             <HeaderTemplate>
21                 <table>
22                     <tr>
23                         <th>编号</th>
24                         <th></th>
25                         <th>级别</th>
26                         <th>操作</th>
27                     </tr>
28             </HeaderTemplate>
29             <ItemTemplate>
30                 <tr>
31                     <td><%#Eval("ID") %></td>
32                     <td><%#Eval("Name") %></td>
33                     <td><%#Eval("Pid") %></td>
34                     <td>
35                         <asp:LinkButton Text="修改" runat="server" ID="LinkUpdate" OnClick="LinkUpdate_Click" CommandArgument='<%#Eval("ID") %>'  />
36                     </td>
37                 </tr>
38             </ItemTemplate>
39             <FooterTemplate>
40                 </table>
41             </FooterTemplate>
42         </asp:Repeater>
43        
44     </form>
45 </body>
46 </html>
Default.aspx
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace Wolfy.DropDownListDemo
 9 {
10     public partial class Default : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14             DropDownListProvince.DataSource = GetList();
15             DropDownListProvince.DataTextField = "Name";
16             DropDownListProvince.DataValueField = "ID";
17             DropDownListProvince.DataBind();
18             RepeaterList.DataSource = GetList();
19             RepeaterList.DataBind();
20         }
21         protected List<Province> GetList()
22         {
23             List<Province> list = new List<Province>();
24             list.Add(new Province() { ID = 1, Name = "北京", Pid = 0 });
25             list.Add(new Province() { ID = 2, Name = "上海", Pid = 0 });
26             list.Add(new Province() { ID = 3, Name = "河南", Pid = 0 });
27             list.Add(new Province() { ID = 4, Name = "河北", Pid = 0 });
28             list.Add(new Province() { ID = 5, Name = "湖南", Pid = 0 });
29             list.Add(new Province() { ID = 6, Name = "湖北", Pid = 0 });
30             return list;
31         }
32 
33         protected void LinkUpdate_Click(object sender, EventArgs e)
34         {
35             LinkButton link = sender as LinkButton;
36             if (link != null)
37             {
38                 int id = Convert.ToInt32(link.CommandArgument);
39                 Province pro = GetList().Find(a => a.ID == id);
40                 this.LiteralID.Text = pro.ID.ToString();
41                 this.LiteralLevel.Text = pro.Pid.ToString();
42                 //DropDownList的index是从零开始的 而绑定的数据的ID是从1开始的,所以为了对应需减一
43                 //改变默认选中项
44                 this.DropDownListProvince.SelectedIndex = pro.ID-1;
45             }
46         }
47     }
48 }

方式二

 1         protected void LinkUpdate_Click(object sender, EventArgs e)
 2         {
 3             LinkButton link = sender as LinkButton;
 4             if (link != null)
 5             {
 6                 int id = Convert.ToInt32(link.CommandArgument);
 7                 Province pro = GetList().Find(a => a.ID == id);
 8                 this.LiteralID.Text = pro.ID.ToString();
 9                 this.LiteralLevel.Text = pro.Pid.ToString();
10                 //方式二
11                 ListItem item = DropDownListProvince.Items.FindByText(pro.Name);
12                 if (item != null)
13                 {
14                     //防止出现多选的情况,将选中项 清除
15                     DropDownListProvince.ClearSelection();
16                     item.Selected = true;
17                 }
18             }
19         }

当时lz载跟头的地方就是报DropDownList出现多选的bug,很无语,这里没办法还原那个bug的场景了,就这样记录一下吧。在报出现那个错误时,将选中项清除就可以了。

总结

工作中,遇到的一个小bug,总结一下,提醒自己以后不要再犯这样的低级错误,无法饶恕。

Demo下载:http://pan.baidu.com/s/1sjJcutf

原文地址:https://www.cnblogs.com/wolf-sun/p/3664771.html