how to find checked radio button in a gridview?

Mahdi Hesari

how to find checked radio button? there is a hatml input with type of radio and has 4 options to select called o1 o2 o3 and o4. i can access the radio buttons with no problem. how should i check which option is selected?

<asp:GridView OnRowCommand="SelectedPollGridView_RowCommand" ID="SelectedPollGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="PollID" DataSourceID="SelectedPollSqlDataSource">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <p runat="server" id="HeaderPTag" class="text-center"><small><%#Eval("Header") %></small></p>
            </HeaderTemplate>
            <ItemTemplate>
                <p runat="server" id="BodyPTag" class="text-right"><%#Eval("Body") %></p>
                <asp:Label Visible="false" ID="PollIDLabel" runat="server" Text='<%#Eval("PollID") %>'></asp:Label>

                <div runat="server" id="MainDiv">
                    <div runat="server" id="O1Div">
                        <label runat="server" id="O1Label">
                            <input runat="server" type="radio" name="OptionsOne" id="O1" value='<%#Eval("PollID") %>'>
                            <%#Eval("O1") %>
                        </label>
                    </div>
                    <div runat="server" id="O2Div">
                        <label runat="server" id="O2Label">
                            <input runat="server" class="pull-right" type="radio" name="OptionsTwo" id="O2" value='<%#Eval("PollID") %>'>
                            <%#Eval("O2") %>
                        </label>
                    </div>
                    <div runat="server" id="O3Div">
                        <label runat="server" id="O3Label">
                            <input runat="server" class="pull-right" type="radio" name="OptionsThree" id="O3" value='<%#Eval("PollID") %>'>
                            <%#Eval("O3") %>
                        </label>
                    </div>
                    <div runat="server" id="O4Div">
                        <label runat="server" id="O4Label">
                            <input runat="server" class="pull-right" type="radio" name="OptionsFour" id="O4" value='<%#Eval("PollID") %>'>
                            <%#Eval("O4") %>
                        </label>
                    </div>
                </div>
                <asp:Button CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="foo" CssClass="btn btn-info" ID="SubmitPollButton" runat="server" Text="ثبت نظر" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SelectedPollSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GUOTSConnectionString %>" SelectCommand="SELECT DISTINCT [PollID], [Header], [Body], [O1], [O1Vis], [O2], [O2Vis], [O3], [O1Cnt], [O2Cnt], [O3Cnt], [O3Vis], [O4], [O4Cnt], [O4Vis], [PollDate] FROM [Poll] ">
<SelectParameters>
    <asp:QueryStringParameter Name="PollID" QueryStringField="PollID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

and im using this code to access it:

protected void SelectedPollGridView_RowCommand(object sender, GridViewCommandEventArgs e)

{
    if (e.CommandName == "foo")
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection.      
        GridViewRow row = SelectedPollGridView.Rows[index];

        System.Web.UI.HtmlControls.HtmlInputRadioButton O1Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O1");
        System.Web.UI.HtmlControls.HtmlInputRadioButton O2Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O2");
        System.Web.UI.HtmlControls.HtmlInputRadioButton O3Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O3");
        System.Web.UI.HtmlControls.HtmlInputRadioButton O4Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O4");
        Label myPollIDLAbel = (Label)row.FindControl("PollIDLabel");
    }
}

now how should i check which radio button has selected?

thank you very much.

Alice

HtmlInputRadioButton has a properties names Checked (return boolean type), you could use this prop. to check which radio button has selected.

For sample, after you get the radio button control in RowCommand event handler, then you have to check the prop. like this:

System.Web.UI.HtmlControls.HtmlInputRadioButton O1Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O1");
System.Web.UI.HtmlControls.HtmlInputRadioButton O2Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O2");
System.Web.UI.HtmlControls.HtmlInputRadioButton O3Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O3");
System.Web.UI.HtmlControls.HtmlInputRadioButton O4Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O4");

if(O1Radio.Checked)
{
  //O1Radio is selected.
} 
else if(O2Radio.Checked)
{
  //O2Radio is selected.
}
else if(O3Radio.Checked)
{
  //O3Radio is selected.
}
else if(O4Radio.Checked)
{
  //O4Radio is selected.
}

EDIT

To group radiobuttons, you should set the same name for all radiobuttons in a group:

...
<input runat="server" type="radio" name="Options" id="O1" value='<%#Eval("PollID") %>' />
...
<input runat="server" type="radio" name="Options" id="O2" value='<%#Eval("PollID") %>' />
...
<input runat="server" type="radio" name="Options" id="O3" value='<%#Eval("PollID") %>' />
...
<input runat="server" type="radio" name="Options" id="O4" value='<%#Eval("PollID") %>' />
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to find checked radio button in a gridview?

From Dev

How to set checked="checked" in first radio button?

From Java

Find out whether radio button is checked with JQuery?

From Dev

Android - How to enable a button if a radio button is checked?

From Dev

how to set checked radio button by checking another radio button

From Dev

How can I get the text of radio button if radio button checked?

From Dev

How to render checked radio button in React

From Dev

How to display checked value for radio button collection

From Dev

How to set a certain radio button to checked "true"

From Dev

How to add checked class to a radio button in AngularJS

From Dev

how to check the radio button is checked or not in windows form

From Dev

How to mark a radio button as checked with no action performed?

From Dev

How to set radio button checked initially in AngularJs?

From Dev

how to get checked radio button value in javascript

From Dev

How to get checked radio button in list view?

From Dev

How to add checked class to a radio button in AngularJS

From Dev

How to get radio button checked in AngularJS?

From Dev

How to set a certain radio button to checked "true"

From Dev

How to have a default radio button checked in Ruby?

From Dev

How to save radio button checked on orientation change?

From Dev

how to increase counter on radio button is checked ? Android

From Dev

how to check which radio button is checked in android?

From Dev

How to display the checked radio button value in angularJs

From Dev

How to mark a radio button as checked with no action performed?

From Dev

Radio button checked with JQuery

From Dev

If radio button is checked not running

From Dev

Radio Button Checked not working

From Dev

Find which input radio button is checked in code behind

From Dev

How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

Related Related

  1. 1

    how to find checked radio button in a gridview?

  2. 2

    How to set checked="checked" in first radio button?

  3. 3

    Find out whether radio button is checked with JQuery?

  4. 4

    Android - How to enable a button if a radio button is checked?

  5. 5

    how to set checked radio button by checking another radio button

  6. 6

    How can I get the text of radio button if radio button checked?

  7. 7

    How to render checked radio button in React

  8. 8

    How to display checked value for radio button collection

  9. 9

    How to set a certain radio button to checked "true"

  10. 10

    How to add checked class to a radio button in AngularJS

  11. 11

    how to check the radio button is checked or not in windows form

  12. 12

    How to mark a radio button as checked with no action performed?

  13. 13

    How to set radio button checked initially in AngularJs?

  14. 14

    how to get checked radio button value in javascript

  15. 15

    How to get checked radio button in list view?

  16. 16

    How to add checked class to a radio button in AngularJS

  17. 17

    How to get radio button checked in AngularJS?

  18. 18

    How to set a certain radio button to checked "true"

  19. 19

    How to have a default radio button checked in Ruby?

  20. 20

    How to save radio button checked on orientation change?

  21. 21

    how to increase counter on radio button is checked ? Android

  22. 22

    how to check which radio button is checked in android?

  23. 23

    How to display the checked radio button value in angularJs

  24. 24

    How to mark a radio button as checked with no action performed?

  25. 25

    Radio button checked with JQuery

  26. 26

    If radio button is checked not running

  27. 27

    Radio Button Checked not working

  28. 28

    Find which input radio button is checked in code behind

  29. 29

    How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

HotTag

Archive