WebForm中,设置DropDownList1,Dropdownlist2,and ListBox1;DropDownList1用来读取数据库中大类内容;Dropdownlist2读取小类内容,ListBox1用来显示小类下的相关数据内容。现在的问题是:DropDownList1读取了大类,Dropdownlist2显示了相关的小类;但是Dropdownlist2无法更改SelectedItem;如果将Dropdownlist2的AutoPostBack改为False;可以选取,但是无法把改变传递到ListBox1;
请问各位高手该如何解决?
请问各位高手该如何解决?
解决方案 »
- c#实现一秒钟从串口接收到的数据取一个绘制成一段时间的实时动态的显示
- FORM2中有个状态栏,怎么样把登陆界面(FORM1)中的用户名提取到该状态栏上显示呢?用户名连接后台数据库
- 北京程序员请进
- trackbar问题
- 怎么用程序生成程序?高手请进!
- 关于正则表达式和indexOf的速度比较??
- treeview 问题 找不到文件或程序集名称“Microsoft.Web.UI.WebControls”
- 各位高手我想问下。如何实现定时重新启动WINDOWS服务
- 一个小问题
- 求助!!
- <asp:datalist 的情况下好像用 e.Item.Cells[5].Text="";不行?
- 请问如何用代码实现在一个Access数据库中删除表及修改表名?
if(!Page.IsPostBack)
{
//DropDownList1
try
{
cmd=new SqlCommand("Select cInvCName from InventoryClass where bInvCEnd=0 or(LEN(cInvCCode)=2 and bInvCEnd=1)",Conn);
reader=cmd.ExecuteReader();
while(reader.Read())
{
ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
DropDownList1.Items.Add(item);
}
}
catch(Exception exc)
{
this.Response.Write(exc.Message);
}
finally
{
reader.Close();
}
}
else
{
//DropDownList2
Dropdownlist2.Items.Clear();
try
{
string change=DropDownList1.SelectedItem.Text;
cmd=new SqlCommand("Select cInvCCode from InventoryClass where cInvCName='"+change+"'",Conn);
reader=cmd.ExecuteReader();
reader.Read();
string code=reader.GetValue(0).ToString();
reader.Close();
cmd=new SqlCommand("select cInvCName from InventoryClass where substring(cInvCCode,1,LEN('"+code+"'))='"+code+"' and bInvCEnd=1",Conn);
reader=cmd.ExecuteReader();
while(reader.Read())
{
ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
Dropdownlist2.Items.Add(item);
}
}
catch(Exception exc)
{
this.Response.Write(exc.Message);
}
finally
{
reader.Close();
}
//ListBox1
if(Dropdownlist2.Items.ToString()!="")
{
ListBox1.Items.Clear();
//dropdownlist2.selectedItem.Text(Name)->cInvCCode->cInvName
string change=Dropdownlist2.SelectedItem.Text;
cmd=new SqlCommand("Select cInvCCode from InventoryClass where cInvCName='"+change+"'",Conn);
reader=cmd.ExecuteReader();
reader.Read();
string code=reader.GetValue(0).ToString();
reader.Close();
cmd=new SqlCommand("select cInvName from Inventory where cInvCCode='"+code+"'",Conn);
reader=cmd.ExecuteReader();
while(reader.Read())
{
ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
ListBox1.Items.Add(item);
}
}
}
DropDownList1的AutoPostBack本来就设置的true
SelectedIndexChanged事件代码我没写,不过用上面的代码已经完全可以实现从DropDownList1到DropDownList2的功能
现在的问题是DropDownList2的值能显示,但是无法选取,不论选什么值都显示第一行的值。
to:baobei7758(陵少)
不是很清楚你所说的方法,可以说清楚一些吗?
的内容时,重载了一次页面,然后执行了从list1选取list2内容的语句;不知对不对,该如何处理;请各位大侠指点。多谢多谢
http://blog.csdn.net/athossmth/archive/2005/03/20/324911.aspx
DropDownList1和DropDownList2的AutoPostBack都设置为true。
在page_Load的if(!Page.IsPostBack)中填充DropDownList1;
在DropDownList1的SelectedIndexChanged事件中填充DropDownList2;
在DropDownList2的SelectedIndexChanged事件中填充ListBox1。
你再好好想想。
个人意见。
每次提交都会pageload,
我想selectedindexchanged事件是可行的,我下午试试多谢各位