我用dropdownlist控件做了数据绑定,但却显示不出结果来

sandywind 2004-11-01 11:48:31
我用一个dropdownlist 控件做选择,另一个相应做出显示,但却显示不出效果来。各位高手帮忙找找原因。
public void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strvalue="";
for (int inti=0; inti< DropDownList1.Items.Count-1;inti++)
{
if(DropDownList1.Items[inti].Selected)
{
strvalue=DropDownList1.Items[inti].Value;}
}
int intvalue=int.Parse(strvalue);
string connectstring= "user id=sa;password=;initial catalog=vinegar;data source=thumb;Connect Timeout=30 ";
SqlConnection sqlConnection1=new SqlConnection(connectstring);
sqlConnection1.Open();
SqlDataAdapter sqlCommand1= new SqlDataAdapter("checkoption",sqlConnection1);
sqlCommand1.SelectCommand.CommandType=CommandType.StoredProcedure;
sqlCommand1.SelectCommand.Parameters.Add("@value", intvalue);
DataSet dataSet1=new DataSet();
sqlCommand1.Fill(dataSet1,"NocheckList");
sqlConnection1.Close();
DropDownList2.DataSource=dataSet1.Tables["NocheckList"].DefaultView ;
DropDownList2.DataBind();
}
附存储过程:
CREATE procedure dbo.checkoption
@value int
AS
if @value=0(
select AccesoryID from NoCheckList )
if @value=1(
select AccesoryName from NoCheckList)
if @value=2(
select Batch from NoCheckList)
if @value=3(
select vendor from NoCheckList)
if @value=4(
select ArrivalDate from NoCheckList)
if @value=5(
select TimeLimits from NoCheckList)
GO

...全文
237 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jinniu 2004-11-05
  • 打赏
  • 举报
回复
页面上设置DropDownList1的AutoPostBack 属性为true!
Jinniu 2004-11-05
  • 打赏
  • 举报
回复
DropDownList1.AutoPostBack = true;
brightheroes 2004-11-04
  • 打赏
  • 举报
回复
你既然只有一个字段,为什么要让DropDownList2.DataTextField="Batch";
你这个Batch字段不存在的话,还怎么显示数据?
brightheroes 2004-11-02
  • 打赏
  • 举报
回复
我发现,无论你是什么情况,你的 DropDownList2.DataTextField="Batch";
你的存储过程中,是否所有的情况,都选择出了这个Batch字段呢?
好像是没有
你检查一下
sandywind 2004-11-02
  • 打赏
  • 举报
回复
回 brightheroes(闭关|那一剑的风情)
我改了程序
public void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strvalue="";
for (int inti=0; inti< DropDownList1.Items.Count-1;inti++)
{
if(DropDownList1.Items[inti].Selected)
{
strvalue=DropDownList1.Items[inti].Value;}
}
int intvalue=int.Parse(strvalue);
string connectstring= "user id=sa;password=;initial catalog=vinegar;data source=thumb;Connect Timeout=30 ";
SqlConnection sqlConnection1=new SqlConnection(connectstring);
sqlConnection1.Open();
SqlDataAdapter sqlDataAdapter1= new SqlDataAdapter("checkoption",sqlConnection1);
sqlDataAdapter1.SelectCommand.CommandType=CommandType.StoredProcedure;
sqlDataAdapter1.SelectCommand.Parameters.Add("@value", intvalue);
DataSet dataSet1=new DataSet();
sqlDataAdapter1.Fill(dataSet1,"Items");
sqlConnection1.Close();
DropDownList2.DataSource=dataSet1.Tables["NocheckList"].DefaultView ;
switch(intvalue)
{
case(0):
DropDownList2.DataTextField="AccesoryID";
DropDownList2.DataValueField="Batch";
break;
case(1):
DropDownList2.DataTextField="AccesoryName";
DropDownList2.DataValueField="Batch";
break;
case(2):
DropDownList2.DataTextField="Batch";
DropDownList2.DataValueField="Batch";
break;
case(3):
DropDownList2.DataTextField="Vendor";
DropDownList2.DataValueField="Batch";
break;
case(4):
DropDownList2.DataTextField="ArrivalDate";
DropDownList2.DataValueField="Batch";
break;
case(5):
DropDownList2.DataValueField="AccesoryID";
DropDownList2.DataTextField="Batch";
break;
}
DropDownList2.DataBind();
}
但在第二个dropdownlist2还是空空的,编译也通过了。是什么原因让它无法正常显示。
brightheroes 2004-11-01
  • 打赏
  • 举报
回复
为什么不行?
你跟踪一下程序,看看有没有选出来数据
看看int intvalue=int.Parse(strvalue);
是多少
sandywind 2004-11-01
  • 打赏
  • 举报
回复
回 brightheroes(闭关|那一剑的风情) 用了还是不行。
smilesad 2004-11-01
  • 打赏
  • 举报
回复
dropdownlist的 AutoPostBack的属性要设置为true
brightheroes 2004-11-01
  • 打赏
  • 举报
回复
修改存储过程如下:无论你选择什么,都返回xx这个字段
CREATE procedure dbo.checkoption
@value int
AS
if @value=0(
select AccesoryID as xx from NoCheckList )
if @value=1(
select AccesoryName as xx from NoCheckList)
if @value=2(
select Batch as xx from NoCheckList)
if @value=3(
select vendor as xx from NoCheckList)
if @value=4(
select ArrivalDate as xx from NoCheckList)
if @value=5(
select TimeLimits as xx from NoCheckList)
GO


DropDownList2.DataSource=dataSet1.Tables["NocheckList"].DefaultView ;
DropDownList2.DataTextField="xx";
DropDownList2.DataValueField="xx";
DropDownList2.DataBind();
sandywind 2004-11-01
  • 打赏
  • 举报
回复
是,但我想用dropdownlist2显示不同的字段。
hiying 2004-11-01
  • 打赏
  • 举报
回复
dropdownlist.DataSource=Rs;
dropdownlist.DataTextField="要显示的字段";
dropdownlist.DataValueField="要显示的字段的值";
dropdownlist.DataBind();
AustinLei 2004-11-01
  • 打赏
  • 举报
回复
要设置dropdownlist的DataTextField和DataValueField属性。
jsyzfeb 2004-11-01
  • 打赏
  • 举报
回复
dropdownlist的 AutoPostBack的属性设置为true了吗?
vzxq 2004-11-01
  • 打赏
  • 举报
回复
dropdownlist的DataTextField和DataValueField
lhcoolhacker 2004-11-01
  • 打赏
  • 举报
回复
dropdownlist.DataTextField="要显示的字段";
dropdownlist.DataValueField="要显示的字段的值";
正解
rickjelly2004 2004-11-01
  • 打赏
  • 举报
回复
dropdownlist.DataTextField="要显示的字段";
dropdownlist.DataValueField="要显示的字段的值";

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧