All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
DropDownList联动简单实例_[Asp.Net教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/1/30 1:01:33 Browse times: 321 Comment times: 0

DropDownList联动简单实例_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

DropDownList联动简单实例_[Asp.Net教程] 在编程中选中某项的方法:
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" %>





无标题页












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




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.