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

时间:2023-03-08 17:27:28

引言

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

一个例子

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

[Asp.net]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