如何在gridview中找到选中的单选按钮? [英] how to find checked radio button in a gridview?

查看:23
本文介绍了如何在gridview中找到选中的单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到选中的单选按钮?有一个带有无线电类型的 hatml 输入,有 4 个选项可供选择,称为 o1 o2 o3 和 o4.我可以毫无问题地访问单选按钮.我应该如何检查选择了哪个选项?

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?

非常感谢.

推荐答案

HtmlInputRadioButton 有一个属性名称 Checked(返回布尔类型),你可以使用这个属性.检查选择了哪个单选按钮.

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

例如,在 RowCommand 事件处理程序中获得单选按钮控件后,您必须检查道具.像这样:

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.
}

编辑

要将单选按钮分组,您应该为组中的所有单选按钮设置相同的名称:

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") %>' />
...

这篇关于如何在gridview中找到选中的单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆