RadioButtonList绑定数据源问题 RadioButtonList 4个选项 :好,中上,中,差。 数据库里有 一个字段,字段值为 上面 4个。如何 读取数据 让相应的选项被选中? 修改相应选项,点击保存,如何 将修改的值 修改到数据库? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 For i = 0 To 9 rd1.Items.Add(i) Next rd1.Text = "2"简单来说可以这样, 你绑定的Value值也是用同个字段来绑定的吗?如果是的话RadioButtonList1.SelectedValue = 读取出来的值;不是的话读取Value绑定的值赋给SelectedValue;修改保存到数据库,先读取出来在修改 RadioButtonList1.SelectedItem.Text//Text值 RadioButtonList1.SelectedValue//Value值 直接用下面的这种判断: if (dt.Rows[0]["UserSelect"].ToString() == "好") { rbtl_Status.SelectedIndex = 0; }else if (dt.Rows[0]["UserSelect"].ToString() == "中上") { rbtl_Status.SelectedIndex = 1; }else if (dt.Rows[0]["UserSelect"].ToString() == "中") { rbtl_Status.SelectedIndex = 2; } else { rbtl_Status.SelectedIndex = 3; }修改数据库:string UserSelect= rbtl_Status.SelectedItem.Text;string mySQL="Update Table1 set UserSelect='"+UserSelect+"' where ID="+ServiceID; <asp:gridview ID="GridView2"><asp:TemplateField HeaderText="单元状态"> <itemtemplate> <asp:RadioButtonList ID="rbtnlist1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem value="好" text="好" /> <asp:ListItem value="中上" text="中上" /> <asp:ListItem value="中" text="中" /> <asp:ListItem value="差" text="差" /> </asp:RadioButtonList> </itemtemplate> <itemstyle width="150px" /> </asp:TemplateField></gridview>后台代码: DataTable t = GetDataTable(); GridView2.DataSource = t; GridView2.DataBind(); RadioButtonList rbt = this.GridView2.FindControl("rbtnlist1") as RadioButtonList; for (int i = 1; i <= t.Rows.Count; i++) { if (t.Rows[i]["DYZT"].ToString() == "好") { rbt.SelectedIndex = 0; } else if (t.Rows[i]["DYZT"].ToString() == "中上") { rbt.SelectedIndex = 1; } else if (t.Rows[i]["DYZT"].ToString() == "中") { rbt.SelectedIndex = 2; } else { rbt.SelectedIndex = 3; } }“rbt.SelectedIndex = 0;”未将对象引用设置到对象的实例。 yanlele424 来 帮我看看 是怎么回事 <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Text="好" Value="1"></asp:ListItem> <asp:ListItem Text="中上" Value="2"></asp:ListItem> <asp:ListItem Text="中" Value="3"></asp:ListItem> <asp:ListItem Text="差" Value="4"></asp:ListItem> </asp:RadioButtonList>protected void Page_Load(object sender, EventArgs e) { this.RadioButtonList1.SelectedValue = "2"; //2可从数据库中读取。 } <asp:RadioButtonList ID="rbtnlist1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem value="好" text="好" /> <asp:ListItem value="中上" text="中上" /> <asp:ListItem value="中" text="中" /> <asp:ListItem value="差" text="差" /> </asp:RadioButtonList>我数据库中存放的的就是 好,中上,中,差 四个之中的一个,RadioButtonList rbt = this.GridView2.FindControl("rbtnlist1") as RadioButtonList; for (int i = 0; i <= t.Rows.Count;i++) { string item = t.Rows[i]["DYZT"].ToString(); if (rbt.Items[i].Text == item) { rbt.Items[i].Selected = true; } }(t 是数据源)为什么 到 if判断的时候 rbt 是个空? 在 protected void gvProjectText_RowDataBound(object sender, GridViewRowEventArgs e)事件中{ RadioButtonList rbt = e.Row.FindControl(“rbtnlist1") as RadioButtonList } rbt 好像 依然 是个空啊... ... RadioButtonList rbt = this.GridView2.FindControl("rbtnlist1") as RadioButtonList;如果这个找不到 就不是下面代码的问题了你先找Gridview里绑定RadioButtonList 的代码然后再this.RadioButtonList1.SelectedValue =取的值 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { RadioButtonList rbl = e.Row.FindControl("RadioButtonList2") as RadioButtonList; rbl .RadioButtonList1.SelectedValue = 数据库中读取的值;; } 在 protected void gvProjectText_RowDataBound(object sender, GridViewRowEventArgs e)事件中{ if (e.Row.RowType == DataControlRowType.DataRow) { RadioButtonList rbt = e.Row.FindControl(“rbtnlist1") as RadioButtonList; }} 后台通过RadioButtonList.SelectValue来设置和获取单选框中选中的值(value) 是运行完 那一句 得出的 结果是 rbt是null 所以后面的就没办法了。用这个if (e.Row.RowType == DataControlRowType.DataRow) 判断 后的结果 是flase... 悲催啊 在后台 怎么绑定他的 radio啊 给点代码吧... ... 我快要疯了 <asp:TemplateField HeaderText="单元状态"> <itemtemplate> <asp:RadioButtonList ID="rbtnlist1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem value="好" text="好" width="50"/> <asp:ListItem value="中上" text="中上" width="50"/> <asp:ListItem value="中" text="中" width="50"/> <asp:ListItem value="下" text="下" width="50"/> </asp:RadioButtonList> </itemtemplate> <itemstyle width="150px" /> </asp:TemplateField>protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { RadioButtonList rbt = e.Row.FindControl("rbtnlist1") as RadioButtonList; } 为什么 rbt 是空??? 谁能帮忙解决一下啊 我快要疯了 哪位能详细讲解下:html服务器控件,和asp服务器控件的区别? JS脚本跳转页面后丢失SESSION 怎么监听一个网页,如果目标网站指定内容有更新,本站程序自动获取更新的内容? 求稳程序中怎样判断数据库是否更新,如果更新重新显示数据. WinForm 自定义控件 绘制菱形 vs2005使用配置文件跟踪语言首选项问题 URL中文件名和目录名加#号后,如何通过地址栏访问或下载到? 偶想做个下载系统,各位大侠给点思路! 从数据库中检索出的数据赋值问题! 处理文件的问题 asp.net 仿京东商城商品分类效果 急 !!寻求比较简单的导航菜单
rd1.Items.Add(i)
Next
rd1.Text = "2"简单来说可以这样,
RadioButtonList1.SelectedValue = 读取出来的值;
不是的话读取Value绑定的值赋给SelectedValue;修改保存到数据库,先读取出来在修改
RadioButtonList1.SelectedItem.Text//Text值
RadioButtonList1.SelectedValue//Value值
if (dt.Rows[0]["UserSelect"].ToString() == "好")
{
rbtl_Status.SelectedIndex = 0;
}
else if (dt.Rows[0]["UserSelect"].ToString() == "中上")
{
rbtl_Status.SelectedIndex = 1;
}
else if (dt.Rows[0]["UserSelect"].ToString() == "中")
{
rbtl_Status.SelectedIndex = 2;
}
else
{
rbtl_Status.SelectedIndex = 3;
}
修改数据库:
string UserSelect= rbtl_Status.SelectedItem.Text;
string mySQL="Update Table1 set UserSelect='"+UserSelect+"' where ID="+ServiceID;
<itemtemplate>
<asp:RadioButtonList ID="rbtnlist1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem value="好" text="好" />
<asp:ListItem value="中上" text="中上" />
<asp:ListItem value="中" text="中" />
<asp:ListItem value="差" text="差" />
</asp:RadioButtonList>
</itemtemplate>
<itemstyle width="150px" />
</asp:TemplateField></gridview>
后台代码:
DataTable t = GetDataTable();
GridView2.DataSource = t;
GridView2.DataBind();
RadioButtonList rbt = this.GridView2.FindControl("rbtnlist1") as RadioButtonList;
for (int i = 1; i <= t.Rows.Count; i++)
{
if (t.Rows[i]["DYZT"].ToString() == "好")
{
rbt.SelectedIndex = 0;
}
else if (t.Rows[i]["DYZT"].ToString() == "中上")
{
rbt.SelectedIndex = 1;
}
else if (t.Rows[i]["DYZT"].ToString() == "中")
{
rbt.SelectedIndex = 2;
}
else
{
rbt.SelectedIndex = 3;
}
}
“rbt.SelectedIndex = 0;
”未将对象引用设置到对象的实例。
yanlele424 来 帮我看看 是怎么回事
<asp:ListItem Text="好" Value="1"></asp:ListItem>
<asp:ListItem Text="中上" Value="2"></asp:ListItem>
<asp:ListItem Text="中" Value="3"></asp:ListItem>
<asp:ListItem Text="差" Value="4"></asp:ListItem>
</asp:RadioButtonList>protected void Page_Load(object sender, EventArgs e)
{
this.RadioButtonList1.SelectedValue = "2";
//2可从数据库中读取。
}
<asp:ListItem value="好" text="好" />
<asp:ListItem value="中上" text="中上" />
<asp:ListItem value="中" text="中" />
<asp:ListItem value="差" text="差" />
</asp:RadioButtonList>我数据库中存放的的就是 好,中上,中,差 四个之中的一个,
RadioButtonList rbt = this.GridView2.FindControl("rbtnlist1") as RadioButtonList;
for (int i = 0; i <= t.Rows.Count;i++)
{
string item = t.Rows[i]["DYZT"].ToString();
if (rbt.Items[i].Text == item)
{
rbt.Items[i].Selected = true;
}
}
(t 是数据源)为什么 到 if判断的时候 rbt 是个空?
RadioButtonList rbt = e.Row.FindControl(“rbtnlist1") as RadioButtonList
}
如果这个找不到 就不是下面代码的问题了
你先找Gridview里绑定RadioButtonList 的代码
然后再
this.RadioButtonList1.SelectedValue =取的值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
RadioButtonList rbl = e.Row.FindControl("RadioButtonList2") as RadioButtonList;
rbl .RadioButtonList1.SelectedValue = 数据库中读取的值;;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rbt = e.Row.FindControl(“rbtnlist1") as RadioButtonList;
}
}
<itemtemplate>
<asp:RadioButtonList ID="rbtnlist1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem value="好" text="好" width="50"/>
<asp:ListItem value="中上" text="中上" width="50"/>
<asp:ListItem value="中" text="中" width="50"/>
<asp:ListItem value="下" text="下" width="50"/>
</asp:RadioButtonList>
</itemtemplate>
<itemstyle width="150px" />
</asp:TemplateField>protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
RadioButtonList rbt = e.Row.FindControl("rbtnlist1") as RadioButtonList;
} 为什么 rbt 是空??? 谁能帮忙解决一下啊 我快要疯了