50分求下拉列表框的极其奇怪的问题,大家进来看看啊
我要做的是有刷新的两个下拉框,DropDownList1中除了从数据库中填充以外,又手动增加了四项,DropDownList1的AutoPostBack也设置为了True,可是怪事出来了,当选择
DropDownList1的时候,只有选择从数据库中填充的数据才能激活DropDownList1_SelectedIndexChanged
事件,选择手动增加的那四项(即人民代表大会等四项)是没有用的,不会有任何事件发生,而且明明选择了“人民代表大会”,它的当前项也马上变成第一项“请选择”,不会停留在
“人民代表大会”上,请问大家这是怎么回事呢?
下面是全部代码:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
DownBind1();
}
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string sValue = this.DropDownList1.SelectedValue.ToString();
string sText=this.DropDownList1.SelectedItem.Text;
int sIndex=this.DropDownList1.SelectedIndex;
if (sIndex <4)
{
string gcd= "select 编码,机构名称 from 政府通讯录 where 机构名称1= ' "+sText+ " ' ";
DataTable mytab = Class.DataAccess.GetSqlData (gcd);
this.DropDownList2.DataSource = mytab;
this.DropDownList2.DataValueField = "编码 ";
this.DropDownList2.DataTextField = "机构名称 ";
this.DropDownList2.DataBind();
}
else
{
sValue=sValue.Substring(0,2);
string sql = "SELECT 行政区代码,名称1 FROM 行政编码 WHERE (名称1 IS NOT NULL) AND (名称2 IS NULL) AND (行政区代码 like ' " + sValue+ "% ') " ;
DataTable mytab = Class.DataAccess.GetSqlData (sql);
this.DropDownList2.DataSource = mytab;
this.DropDownList2.DataValueField = "行政区代码 ";
this.DropDownList2.DataTextField = "名称1 ";
this.DropDownList2.DataBind();
}
}
private void DownBind1()
{
string sql = "SELECT DISTINCT 行政区代码,名称 FROM 行政编码 WHERE 名称1 IS NULL ";
DataTable mytab = Class.DataAccess.GetSqlData (sql);
this.DropDownList1.DataSource = mytab;
this.DropDownList1.DataValueField = "行政区代码 ";
this.DropDownList1.DataTextField = "名称 ";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0,new ListItem( "请选择 ", " "));
this.DropDownList1.Items.Insert(1,new ListItem( "中国共产党 ", " "));
this.DropDownList1.Items.Insert(2,new ListItem( "人民代表大会 ", " "));
this.DropDownList1.Items.Insert(3,new ListItem( "政治协商会议 ", " "));
this.DropDownList1.Items.Insert(4,new ListItem( "国家机关 ", " "));
}
------解决方案--------------------你选定自动提交后又刷新了一次`页面嘛!
你调试你的代码`看每步怎么走!就可以检查出问题了!
------解决方案--------------------你将 this.DropDownList1.Items.Insert(0,new ListItem( "请选择 ", " "));
this.DropDownList1.Items.Insert(1,new ListItem( "中国共产党 ", " "));
this.DropDownList1.Items.Insert(2,new ListItem( "人民代表大会 ", " "));
this.DropDownList1.Items.Insert(3,new ListItem( "政治协商会议 ", " "));
this.DropDownList1.Items.Insert(4,new ListItem( "国家机关 ", " "));