Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Find index or row in gridview with RadioButton

Find index or row in gridview with RadioButton

Scheduled Pinned Locked Moved ASP.NET
htmldatabasesysadminhelp
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Franco Cipriano
    wrote on last edited by
    #1

    Hi, I have included a radiobutton on a gridview using HTML control. I tried getting the index of the row selected by the radio button, but I can't figure out how. I tried using the Request.Form["RadiobuttonName"] but it gives me the value on the cell not the row index. Here is the code When I click the button to retrieve the value.

    protected void btnSelect_Click(object sender, EventArgs e)
    {
    string selectedValue = Request.Form["radSelect"];
    int index = Convert.ToInt32(Request.Form["radSelect"]);
    }

    And here is .aspx page

    <asp:GridView ID="grdAll" runat="server" OnRowCommand="MPS_Selected" AutoGenerateColumns="false" OnRowDataBound = "grd_rowdatabound">
    <Columns>
    <asp:BoundField DataField="ID" HeaderText = "" Visible="false" />
    <asp:BoundField DataField="Name" HeaderText = "MPS Account Name" />
    <asp:BoundField DataField="Industry" HeaderText = "Industry" />
    <asp:BoundField DataField="SubIndustry" HeaderText = "Sub-Industry" />
    <asp:BoundField DataField="Geo" HeaderText = "Geo" />
    <asp:BoundField DataField="LocType" HeaderText = "Location" />
    <%--<asp:buttonfield buttontype="Button" commandname="Select" headertext="" text="Select"/>--%>
    <asp:TemplateField HeaderText="Select One">
    <ItemTemplate>
    <input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
    <%--<asp:RadioButton ID = "rdSelect" runat="server" onclick = "RadioCheck(this)" />
    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID")%>' />--%>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>

        </asp:GridView>
    

    Any help will do. Thanks!

    S 1 Reply Last reply
    0
    • F Franco Cipriano

      Hi, I have included a radiobutton on a gridview using HTML control. I tried getting the index of the row selected by the radio button, but I can't figure out how. I tried using the Request.Form["RadiobuttonName"] but it gives me the value on the cell not the row index. Here is the code When I click the button to retrieve the value.

      protected void btnSelect_Click(object sender, EventArgs e)
      {
      string selectedValue = Request.Form["radSelect"];
      int index = Convert.ToInt32(Request.Form["radSelect"]);
      }

      And here is .aspx page

      <asp:GridView ID="grdAll" runat="server" OnRowCommand="MPS_Selected" AutoGenerateColumns="false" OnRowDataBound = "grd_rowdatabound">
      <Columns>
      <asp:BoundField DataField="ID" HeaderText = "" Visible="false" />
      <asp:BoundField DataField="Name" HeaderText = "MPS Account Name" />
      <asp:BoundField DataField="Industry" HeaderText = "Industry" />
      <asp:BoundField DataField="SubIndustry" HeaderText = "Sub-Industry" />
      <asp:BoundField DataField="Geo" HeaderText = "Geo" />
      <asp:BoundField DataField="LocType" HeaderText = "Location" />
      <%--<asp:buttonfield buttontype="Button" commandname="Select" headertext="" text="Select"/>--%>
      <asp:TemplateField HeaderText="Select One">
      <ItemTemplate>
      <input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
      <%--<asp:RadioButton ID = "rdSelect" runat="server" onclick = "RadioCheck(this)" />
      <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID")%>' />--%>
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>

          </asp:GridView>
      

      Any help will do. Thanks!

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      What you have given is not a server side control.

      <input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />

      is a client side html control. 2 things: 1. Convert it into server control using runat="server" to access it on server side. 2. In button select, loop through all the rows of grid, use FindControl to find radiobutton control. See if it is selected, if so use the row and it's values Again, using a radiobutton in gridview is little tricky and needs extra effort to group them together so that only one can be selected at a time. If you face issue, just look for article/question on it on this website itself (shared before with lots of users)

      Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]

      F 1 Reply Last reply
      0
      • S Sandeep Mewara

        What you have given is not a server side control.

        <input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />

        is a client side html control. 2 things: 1. Convert it into server control using runat="server" to access it on server side. 2. In button select, loop through all the rows of grid, use FindControl to find radiobutton control. See if it is selected, if so use the row and it's values Again, using a radiobutton in gridview is little tricky and needs extra effort to group them together so that only one can be selected at a time. If you face issue, just look for article/question on it on this website itself (shared before with lots of users)

        Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]

        F Offline
        F Offline
        Franco Cipriano
        wrote on last edited by
        #3

        Thanks for the reply. I tried using literal, but I am getting an error

        Quote:

        Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

        in this line

        output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");

        S 1 Reply Last reply
        0
        • F Franco Cipriano

          Thanks for the reply. I tried using literal, but I am getting an error

          Quote:

          Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

          in this line

          output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");

          S Offline
          S Offline
          Sandeep Mewara
          wrote on last edited by
          #4

          Franco Cipriano wrote:

          output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");

          You forgot to mention the string to be replaced in {0} Something like:

          string.Format("Hi {0}", "there!"); // gives "Hi there!"

          Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]

          F 1 Reply Last reply
          0
          • S Sandeep Mewara

            Franco Cipriano wrote:

            output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");

            You forgot to mention the string to be replaced in {0} Something like:

            string.Format("Hi {0}", "there!"); // gives "Hi there!"

            Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]

            F Offline
            F Offline
            Franco Cipriano
            wrote on last edited by
            #5

            Thanks so much! problem solved!

            S 1 Reply Last reply
            0
            • F Franco Cipriano

              Thanks so much! problem solved!

              S Offline
              S Offline
              Sandeep Mewara
              wrote on last edited by
              #6

              Good to know! :thumbsup:

              Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups