RadioButtonList 4个选项 :好,中上,中,差。 数据库里有 一个字段,字段值为 上面 4个。如何 读取数据 让相应的选项被选中? 修改相应选项,点击保存,如何 将修改的值 修改到数据库?

解决方案 »

  1.   

     For i = 0 To 9
                rd1.Items.Add(i)
      Next
      rd1.Text = "2"简单来说可以这样,
      

  2.   

    你绑定的Value值也是用同个字段来绑定的吗?如果是的话
    RadioButtonList1.SelectedValue = 读取出来的值;
    不是的话读取Value绑定的值赋给SelectedValue;修改保存到数据库,先读取出来在修改
            RadioButtonList1.SelectedItem.Text//Text值
            RadioButtonList1.SelectedValue//Value值
      

  3.   

    直接用下面的这种判断:
          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;
      

  4.   

    <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;
    ”未将对象引用设置到对象的实例。
      

  5.   


    yanlele424  来 帮我看看  是怎么回事
      

  6.   

      <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可从数据库中读取。
        }
      

  7.   

     <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 是个空?
      

  8.   

    在 protected void gvProjectText_RowDataBound(object sender, GridViewRowEventArgs e)事件中{
        RadioButtonList rbt = e.Row.FindControl(“rbtnlist1") as RadioButtonList
        }
      

  9.   

    rbt 好像 依然 是个空啊... ...
      

  10.   

    RadioButtonList rbt = this.GridView2.FindControl("rbtnlist1") as RadioButtonList;
    如果这个找不到 就不是下面代码的问题了
    你先找Gridview里绑定RadioButtonList 的代码
    然后再
    this.RadioButtonList1.SelectedValue =取的值
      

  11.   

      
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
            RadioButtonList rbl = e.Row.FindControl("RadioButtonList2") as RadioButtonList;
             rbl .RadioButtonList1.SelectedValue = 数据库中读取的值;;
        }
      

  12.   

    在 protected void gvProjectText_RowDataBound(object sender, GridViewRowEventArgs e)事件中{
         if (e.Row.RowType == DataControlRowType.DataRow)
            {
             RadioButtonList rbt = e.Row.FindControl(“rbtnlist1") as RadioButtonList;
             }
    }
      

  13.   

    后台通过RadioButtonList.SelectValue来设置和获取单选框中选中的值(value)
      

  14.   

    是运行完 那一句   得出的 结果是 rbt是null  所以后面的就没办法了。用这个if (e.Row.RowType == DataControlRowType.DataRow) 判断 后的结果 是flase...  悲催啊
      

  15.   

    在后台 怎么绑定他的 radio啊  给点代码吧... ...   我快要疯了
      

  16.   

    <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 是空??? 谁能帮忙解决一下啊  我快要疯了