注册 登录
编程论坛 ASP.NET技术论坛

[求助]随textbox值的改变,而使DropDownList的显示对应的文体??

okkenking 发布于 2007-07-31 09:34, 1875 次点击
DropDownList绑定一个表A
A_id A_man
1 张三
2 李四
3 小明


下拉框有三个选项:张三,李四,小明

在表B也有:
B_id B_man B_CODE
5 张三 67
6 李四 34
7 小明 78

在textbox输入列(B_CODE)的某一值.让DropDownList的文本显示对应的值
例如:输入67时,DropDownList显示"张三",
14 回复
#2
yunj11052007-07-31 09:50
DropDownList1.SelectedValue=textbox1.Text
将DropDownList1的autopostback设置为true
#3
okkenking2007-07-31 10:05

你的方法不行啊??

帮帮忙啊

#4
stggg2007-07-31 10:26
回复:(okkenking)[求助]随textbox值的改变,而使Dro...
也可以用javascript

TextBox 添加onchange事件(原事件里没有)
function changedrop()
{
try{
var a = document.getElementById(TextBox的ID).value;
document.getElementById(下拉框的ID).value = a;
}
catch(e){document.getElementById(下拉框的ID).value = 第一项的value;}
}
若不用try...catch,有可能报js错误。。。
#5
sean882007-07-31 10:32
在文本框改变的事件里写
foreach(ListItem li in DropDownList1.Items)
{
if(li.Text.Equals(textbox1.Text))
{
li.Selected=true;
}
}
#6
okkenking2007-07-31 10:36
下拉框的文体对应表A和表B的ID是不同的
#7
stggg2007-07-31 10:54
回复:(okkenking)下拉框的文体对应表A和表B的ID是不...

下拉框的 Text 和 value 都用 A_man
。。。。。。

A表和B表总应该有相同的字段吧,把它当作value


#8
okkenking2007-07-31 11:24

哦,我试下可以不
非常感谢

#9
okkenking2007-08-01 08:03
以下是引用stggg在2007-7-31 10:26:15的发言:
也可以用javascript

TextBox 添加onchange事件(原事件里没有)
function changedrop()
{
try{
var a = document.getElementById(TextBox的ID).value;
document.getElementById(下拉框的ID).value = a;
}
catch(e){document.getElementById(下拉框的ID).value = 第一项的value;}
}
若不用try...catch,有可能报js错误。。。

第一项的value,指的是什么啊??
请教

#10
jyoseyi2007-08-01 09:49
1. 拖一个文本框TextBox1 和 DropDownList1(自己把数据绑定上去)
2. 把TextBox1 右件 属性 Autopostback设置为true
3. 双击TextBox1 把以下代码放入双击后的事件里

for (int i = 0; i < DropDownList1.Items.Count; i++)
if (TextBox1.Text == DropDownList1.Items[i].ToString())
{
DropDownList1.Text = TextBox1.Text;
}
4. 运行,在文本框中输入DropDownList1中的 某个东西完后,鼠标离开文本框,看DropDownList1的的文本变了没有,OK

#11
okkenking2007-08-01 10:07

DropDownList绑定一个表A
A_id A_man
1 张三
2 李四
3 小明


下拉框有三个选项:张三,李四,小明

在表B也有:
B_id B_man B_CODE
5 张三 67
6 李四 34
7 小明 78

在textbox输入列(B_CODE)的某一值.让DropDownList的文本显示对应的值
例如:输入67时,DropDownList显示"张三",

非常感谢jyoseyi



""""例如:输入67时,DropDownList显示"张三","""""""不过还是没有做到这一步啊

#12
okkenking2007-08-01 10:09
for(int i=0;i<this.DropDownList1.Items.Count;i++)
{
if(TextBox1.Text==DropDownList1.SelectedValue)
{
this.DropDownList1.Items[i].Selected=true;
break;
}
}


这样也不行.............
#13
okkenking2007-08-01 10:13

DropDownList绑定一个表A
A_id A_man
67 张三
34 李四
78 小明


下拉框有三个选项:张三,李四,小明

在表B也有:
B_man B_CODE
张三 67
李四 34
小明 78



我把A_id与B_CODE设为相同,还是不行哦

#14
jyoseyi2007-08-01 11:03

你绑定DropDownList1的时候


sqlconnection con =new sqlconnection ()//自己完善
string strsql = "select * from A"; //A表
SqlDataAdapter da = new SqlDataAdapter(strsql,con);
DataSet ds = new DataSet();
da.Fill(ds);


//=======================关键

DropDownList1.DataTextField = "A_ma";
DropDownList1.DataValueField = "A_id";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();


//然后就可以了,


for(int i=0;i<this.DropDownList1.Items.Count;i++)
{
if(TextBox1.Text.Trim()==DropDownList1.Items[i].Value.ToString())
{
this.DropDownList1.Items[i].Selected=true;
break;
}
}

#15
jyoseyi2007-08-01 11:04
这里写掉了
DropDownList1.DataTextField = "A_man";
1