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

[求助]怎么让RadioButtonList的选中项随着对应的值改变?

606liutong 发布于 2007-07-27 10:03, 4486 次点击

从数据库中读取数据COM1

RadioButtonList对应的选中的值也是COM1

从数据库中读取数据COM2

RadioButtonList对应的选中的值也是COM2

怎么实现?以前实现过,现在忘了

//RadioButtonList的绑定
//this.OnoffPort.SelectedValue = reader["ONOFFPORT"].ToString();--错误的

9 回复
#2
cyyu_ryh2007-07-27 10:30
你绑定时用同一个字段绑撒,索引和显示的文本用同一个字段
#3
冰彩虹2007-07-27 10:59
设置RadioButtonList.Items集合里的ListItem的Value值为数据库里的可能性值

然后this.OnoffPort.SelectedValue = reader["ONOFFPORT"].ToString();
#4
606liutong2007-07-27 11:05
就是那样弄的啊

RadioButtonList 有两个值 COM1 COM2 数据库表中也有两个值 COM1 COM2

//this.OnoffPort.SelectedValue = reader["ONOFFPORT"].ToString();--错误的

这样写是错误的
#5
冰彩虹2007-07-27 11:09
报什么错...

确定reader["ONOFFPORT"].ToString()读出来的值在RadioButtonList的ListItem值集合里存在?
#6
606liutong2007-07-27 12:09

确定

[IMG]F:\123.jpg[/IMG]

已经写this.OnoffPort.SelectedValue = reader["ONOFFPORT"].ToString();了

就是不绑定
#7
jyoseyi2007-07-27 12:36

//this.OnoffPort.SelectedValue = reader["ONOFFPORT"].ToString();--错误的




SqlCommand com = new SqlCommand("select * from Picture", con);//con自己写连接

SqlDataReader sdr = com.ExecuteReader();

this.RadioButtonList1.DataTextField = "title";//字段 title
this.RadioButtonList1.DataValueField = "PictureID";//字段 PictureID
RadioButtonList1.DataSource = sdr;
RadioButtonList1.DataBind();

#8
606liutong2007-07-27 13:40

我不是那个意思

我的意思是 怎么让RadioButtonList中选中状态的值 和数据库中读取出来的值一样

反正是RadioButtonList 的Select 的那个值随着 从数据库读取的值一样 ,

这是一个有点小弯路的绑定,敬请高手赐教!!!

#9
jyoseyi2007-07-27 14:57
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedItem.Selected==true)
{
string name = RadioButtonList1.SelectedItem.Text;// name 文本值
      string values= RadioButtonList1.SelectedItem.Value;//value 值



Label1.Text=name
// Label1.Text=values
     //想要哪个自己挑
}
}
#10
606liutong2007-07-27 15:18
你刚好写反

你这样的参照物是RadioButtonList

而我要的参照物是 数据库中的值

唉。。。郁闷。。。
1