在編程中選中某項(xiàng)的方法:
DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue(st.Year.ToString()).Selected = true;
如果不清空選中項(xiàng)的話,會(huì)出現(xiàn):不能在 DropDownList 中選擇多個(gè)項(xiàng)。
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);
}
}
不過我遇到的不是這個(gè)問題,是個(gè)Bug,關(guān)機(jī)再開就能運(yùn)行,奇怪。而且VS2005IDE鍵位還變了,刪除行變成了Ctrl+Y(VB的習(xí)慣),恢復(fù)預(yù)置環(huán)境就好了。
看來遇到問題還是要冷靜:可以采用的處理方法是:祈禱一句,罵一句,睡一覺,關(guān)機(jī)一次。
下面附一個(gè)最基本的實(shí)例。主要,要實(shí)現(xiàn)聯(lián)動(dòng)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>無標(biāo)題頁</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;
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。