RadioButtonList获取选中值

zxcvfsds 2011-08-29 11:31:08
如题:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRadioList();
}
}
//绑定radiolist
private void BindRadioList()
{
this.RadioButtonList1.DataSource = ds.Tables[0];
this.RadioButtonList1.DataTextField = "字段1";
this.RadioButtonList1.DataValueField = "字段2";
this.RadioButtonList1.DataBind();
this.RadioButtonList1.SelectedIndex = 0;

protected void Button1_Click(object sender, EventArgs e)

string text = this.RadioButtonList1.SelectedItem.Text.ToString();
string value = this.RadioButtonList1.SelectedValue.ToString();


不管选的是哪个项 获取的总是第一条的数据…… 还请高手指点一二~
...全文
591 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
暖枫无敌 2011-08-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zxcvfsds 的回复:]

很奇怪啊~ 手动绑定几条数据可以获取数据,换成动态查询出来的,选择完后 点击按钮得到的结果就是第一条数据~ 觉得可能和刷新有关系~ 但是刷新 没有执行绑定的方法呀~ 晕乎乎的~
[/Quote]
RadionButtonList控件的AutoPostBack你设置了吧???
zxcvfsds 2011-08-29
  • 打赏
  • 举报
回复
很奇怪啊~ 手动绑定几条数据可以获取数据,换成动态查询出来的,选择完后 点击按钮得到的结果就是第一条数据~ 觉得可能和刷新有关系~ 但是刷新 没有执行绑定的方法呀~ 晕乎乎的~
huangwenquan123 2011-08-29
  • 打赏
  • 举报
回复
木有问题啊
    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadioButtonList1.DataSource = getTable();
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind();
RadioButtonList1.SelectedIndex = 0;
}
}
public DataTable getTable()
{
string[] name = { "张三", "李四", "王五", "赵六" };
DataTable dt = new DataTable("");
dt.Columns.Add("ID", typeof(System.Int32));
dt.Columns.Add("Name", typeof(System.String));
dt.Columns.Add("Number", typeof(System.Double));
for (int i = 0; i < name.Length; i++)
{
DataRow row = dt.NewRow();
row[0] = i + 1;
row[1] = name[i];
row[2] = 12.3;
dt.Rows.Add(row);
}
return dt;
}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(RadioButtonList1.SelectedItem.Text.ToString());
Response.Write(RadioButtonList1.SelectedValue);
}
zxcvfsds 2011-08-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
每次baind都是 this.RadioButtonList1.SelectedIndex = 0;
跟踪看看每次的SelectedIndex是多少,是否还有其他地方出发了Bind
[/Quote]

this.RadioButtonList1.SelectedIndex = 0;
去掉这句话也测试过,还是一样的结果~

只有初始加载的时候和重新上传的时候 调用了这个绑定方法~
暖枫无敌 2011-08-29
  • 打赏
  • 举报
回复
你加这一句干嘛???

this.RadioButtonList1.SelectedIndex = 0;

默认选中的就是第一个,去掉就可以了。
bdmh 2011-08-29
  • 打赏
  • 举报
回复
每次baind都是 this.RadioButtonList1.SelectedIndex = 0;
跟踪看看每次的SelectedIndex是多少,是否还有其他地方出发了Bind
zxcvfsds 2011-08-29
  • 打赏
  • 举报
回复
晕死~ 竟然不支持~ 自动回滚了貌似…… 结贴!谢谢各位热心帮助!!
zxcvfsds 2011-08-29
  • 打赏
  • 举报
回复

if (this.selectdatatable().Rows.Count > 0)
{
this.RadioButtonList1.DataSource = GetTabelBySql();
this.RadioButtonList1.DataTextField = "ExcelFile";
this.RadioButtonList1.DataValueField = "ExcelPath";
this.RadioButtonList1.DataBind();
RadioButtonList1.SelectedIndex = 0;
}


很怪异~ 上面这中方法和下面的唯一的区别就是绑定的数据源不一样,上面的是从数据库中查出来的~ 下面的是手动写进去的~ 这种写死的就正常,动态绑定的就不行…… 这怎么也说不通啊~


RadioButtonList1.DataSource = GetTable();
RadioButtonList1.DataTextField = "ExcelFile";
RadioButtonList1.DataValueField = "ExcelPath";
RadioButtonList1.DataBind();
RadioButtonList1.SelectedIndex = 0;
lcc101361 2011-08-29
  • 打赏
  • 举报
回复
就你贴出的代码是没问题的 仔细点检查下你的代码 看有没有调过page的DataBind()方法 还有自己的绑定方法 在处理click 事件之前有重新绑定过。
zxcvfsds 2011-08-29
  • 打赏
  • 举报
回复

<td colspan="2" style="width:900px">
<asp:Panel ID="pnlExcel" runat="server" ScrollBars="Vertical" Height="100px" BorderStyle="Dotted" BorderColor="Gray">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" >
</asp:RadioButtonList>
</asp:Panel>
</td>
<td><asp:Button ID="Button1" runat="server" Text="选择" Width="75px"
onclick="Button1_Click"/></td>
</tr>


前台页面代码 就是这了~

真不知道是哪出了问题还是什么原因 就是取不到对应选择的数据……
lcc101361 2011-08-29
  • 打赏
  • 举报
回复
本机测试木有问题 请将前台代码一起贴上来
暖枫无敌 2011-08-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zxcvfsds 的回复:]

引用 6 楼 taomanman 的回复:
引用 5 楼 zxcvfsds 的回复:

很奇怪啊~ 手动绑定几条数据可以获取数据,换成动态查询出来的,选择完后 点击按钮得到的结果就是第一条数据~ 觉得可能和刷新有关系~ 但是刷新 没有执行绑定的方法呀~ 晕乎乎的~

RadionButtonList控件的AutoPostBack你设置了吧???


没有设置autopostbac……
[/Quote]
那不大可能会出现这种情况啊?那你清理一下浏览器缓存,难道是浏览器欺骗了你的眼睛???
zxcvfsds 2011-08-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 taomanman 的回复:]
引用 5 楼 zxcvfsds 的回复:

很奇怪啊~ 手动绑定几条数据可以获取数据,换成动态查询出来的,选择完后 点击按钮得到的结果就是第一条数据~ 觉得可能和刷新有关系~ 但是刷新 没有执行绑定的方法呀~ 晕乎乎的~

RadionButtonList控件的AutoPostBack你设置了吧???
[/Quote]

没有设置autopostback 为true~

纠结……

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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