<asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="Radio">
                    <ItemTemplate>
                        <asp:RadioButton ID="RadioButton1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>我就是在GridView中的第1列加了一列模板列,里面放了一个RadioButton,后台C#我如何知道选中了哪行的RadioButton

解决方案 »

  1.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      CheckBox cbox = (CheckBox)e.Rows.FindControl("CheckBox1")
      if (cbox.Checked == true)
                    {
                      //.....
                    }         }
      

  2.   

    RadioButton RadioButton1=(RadioButton)GridView1.coloums[0].findControl("RadioButton1");
    自己手写的,逻辑就是这个逻辑,可能有些地方拼错了,自己理解一下吧
      

  3.   

    int rowid = ((GridViewRow)((RadioButton)sender).Parent.Parent).RowIndex;
      

  4.   

    先找到控件  然后后台遍历 判断Checked 是否为true
      

  5.   

    其实我还是比较倾向于这种方式的,但
    Columns[0].FindControl报错了,列没有FindControl的函数
      

  6.   


            /// <summary>
            /// 获取选中记录的Id集合
            /// </summary>
            /// <returns></returns>
            private string get_selected()
            {
                CheckBox cb;
                ArrayList al = new ArrayList();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("del");
                    if (cb.Checked)
                    {
                        al.Add((string)GridView1.Rows[i].Cells[1].Text);
                    }
                }            string ids = "(";
                for (int i = 0; i < al.Count; i++)
                {
                    ids += al[i].ToString();
                    if (i + 1 != al.Count)
                    {
                        ids += ",";
                    }
                }
                ids += ")";            return ids;
            }
      

  7.   


    其实我是想把获取当前RadioButton的按钮放在GridView外边的,所以没有sender,现在在纠结是用JavaScript的Button还是用C#的
      

  8.   


    (RadioButton)GridView1.Rows[i].Cell[0].findControl("RadioButton1");
      

  9.   

    CheckBox cbox = (CheckBox)e.Rows.FindControl("Cchk")
      if (cbox.Checked == true)
                    {
                      //.....
                    }         
    先找到控件 然后后台遍历 判断Checked 是否为true
      

  10.   

    但是我发现个问题,就是用模板列添加进去的RadioButton,即使N个RadioButton填了GroupName也没用
      

  11.   


    Using a RadioButton to select a row in GridViewSome people need radiobutton for selection a row in gridview control and take a action after postback.You should to use this code to resolve this issue.<asp:GridView ID="_grid" runat="server" AutoGenerateColumns="False">
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:PlaceHolder runat="server" ID="_holder" /></ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
      </Columns>
    </asp:GridView>This is code behind:
    partial class SampleGridViewUI : Page
    {
      protected override void OnInit(EventArgs e)
      {
        base.OnInit(e);
        this._grid.RowDataBound += new GridViewRowEventHandler( this.Grid_RowDataBound );
        this._go.Click += new EventHandler( this.Go_Click );
      }
      protected override void OnLoad(EventArgs e)
      {
        base.OnLoad(e);
        if ( ! this.IsPostBack )
        {
          this._grid.DataSource = this.Values;
          this._grid.DataBind();
        }
      }
      public IEnumerable<Group> Values
      {
        get
        {
          yield return new Group(1, "Administrators");
          yield return new Group(2, "Users");
          yield return new Group(3, "Power Users");
        }
      }
      private void Grid_RowDataBound( Object sender, GridViewRowEventArgs e )
      {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
          Group group = (Group)e.Row.DataItem;
          PlaceHolder holder = (PlaceHolder)e.Row.FindControl("_holder");
          String innerhtml = String.Format("<input type='radio' name='sample' value='{0}'>", group.Id);
          holder.Controls.Add(TemplateControl.ParseControl(innerhtml));
        }
      }
      private void Go_Click(Object sender, EventArgs e)
      {
        String line = String.Format( "Id='{0}'", this.Request[ "sample" ] );
        this.Response.Write(line);
      }
      public struct Group
      {
        private Int32 _id;
        private String _name;
        public Group(
          Int32 id,
          String name)
        {
          this._id = id;
          this._name = name;
        }
        public Int32 Id { get { return this._id; } }
        public String Name { get { return this._name; } }
      }
    }In Go_Click private function you should take a action with Group ID in Request[ "Sample" ] variable posted.