[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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM