在編程中選中某項(xiàng)的方法: DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue(st.Year.ToString()).Selected = true;
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); } } 不過(guò)我遇到的不是這個(gè)問(wèn)題,,是個(gè)Bug,,關(guān)機(jī)再開(kāi)就能運(yùn)行,奇怪,。而且VS2005IDE鍵位還變了,,刪除行變成了Ctrl+Y(VB的習(xí)慣),恢復(fù)預(yù)置環(huán)境就好了,。 看來(lái)遇到問(wèn)題還是要冷靜:可以采用的處理方法是:祈禱一句,,罵一句,睡一覺(jué),,關(guān)機(jī)一次,。 下面附一個(gè)最基本的實(shí)例。主要,,要實(shí)現(xiàn)聯(lián)動(dòng)DropDownList1的AutoPostBack屬性要開(kāi),。 Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> Default.aspx.cs<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml" > <head runat="server"> <title>無(wú)標(biāo)題頁(yè)</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> 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; } } |
|
來(lái)自: 悟靜 > 《.net和asp.net》