标识在GridViewRow中选择的单选按钮

本文关键字:单选按钮 选择 GridViewRow 标识 | 更新日期: 2023-09-27 18:24:11

我有一个gridview,其中有6个radionbuttons。单击提交按钮,我需要确定在grid的每一行中选择了哪个radiobutton。现在我使用以下代码:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("OrderID");
        dt.Columns.Add("QuestionID");
        dt.Columns.Add("Rating");
        foreach (GridViewRow gr in grdSurvey.Rows)
        {
            DataRow dr = dt.NewRow();
            dr["QuestionID"] = ((HiddenField)gr.FindControl("hfQuestionID")).Value;
            dr["OrderID"] = ViewState["_ORDERID"].ToString();
            if (((RadioButton)gr.FindControl("rbSD")).Checked)
            {
                dr["Rating"] = "1";
            }
            else if (((RadioButton)gr.FindControl("rbSD")).Checked)
            {
                dr["Rating"] = "2";
            }
            else if (((RadioButton)gr.FindControl("rbDS")).Checked)
            {
                dr["Rating"] = "3";
            }
            else if (((RadioButton)gr.FindControl("rbNAND")).Checked)
            {
                dr["Rating"] = "4";
            }
            else if (((RadioButton)gr.FindControl("rbAS")).Checked)
            {
                dr["Rating"] = "5";
            }
            else if (((RadioButton)gr.FindControl("rbSA")).Checked)
            {
                dr["Rating"] = "6";
            }
            else if (((RadioButton)gr.FindControl("rbNA")).Checked)
            {
                dr["Rating"] = "0";
            }
            dt.Rows.Add(dr);
        }
        int Res = InsertSurveyResponse(dt);
        if (Res > 0)
        {
            Response.Write(Res + " Response Submitted.");
        }
    }

前端如下:

<div id="divForm" runat="server">
    <table width="80%" style="text-align:center;">
        <tr>
            <td>Your feedback is important and we promise it will take less than a minute of your time. All you have to do is – read the questions below and select the rating that you feel best describes your experience with us.
            </td>
        </tr>
        <tr>
            <td>Kindly give us your feedback below
            </td>
        </tr>
        <tr>
            <td>
                <asp:gridview id="grdSurvey" runat="server" autogeneratecolumns="false" clientidmode="static">
                    <Columns>
                        <asp:TemplateField HeaderText="Question">
                            <ItemTemplate>
                                <asp:HiddenField ID="hfQuestionID" runat="server" Value='<%#Eval("QuestionID") %>'></asp:HiddenField>
                                <asp:Label ID="lblQuestion" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Strongly Disagree">
                            <ItemTemplate>
                                <asp:RadioButton ID="rbSD" runat="server" GroupName="Survey" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Disagree Somewhat">
                            <ItemTemplate>
                                <asp:RadioButton ID="rbDS" runat="server" GroupName="Survey" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Neither Agree nor disagree">
                            <ItemTemplate>
                                <asp:RadioButton ID="rbNAND" runat="server" GroupName="Survey" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Agree Somewhat">
                            <ItemTemplate>
                                <asp:RadioButton ID="rbAS" runat="server" GroupName="Survey" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Strongly Agree">
                            <ItemTemplate>
                                <asp:RadioButton ID="rbSA" runat="server" GroupName="Survey" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Not applicable">
                            <ItemTemplate>
                                <asp:RadioButton ID="rbNA" runat="server" GroupName="Survey" Checked="true" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:gridview>
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
            </td>
        </tr>
    </table>
</div>

我需要避免我在提交按钮上使用的if-else,我也不能使用单选按钮列表。请帮忙。我想要服务器端代码。没有jquery或javascript。

标识在GridViewRow中选择的单选按钮

类似:

  var selectedButton = Master.FindControl("grdSurvey").Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);

在页面上添加以下脚本

<script type="text/javascript">
 function GetAge() {
  var Row1 = $("input:radio[id$='rbSD']:checked").val();
  var Row2 = $("input:radio[id$='rbDS']:checked").val();
  var Row3 = $("input:radio[id$='rbNAND']:checked").val();
  var Row4 = $("input:radio[id$='rbAS']:checked").val();
  var Row5 = $("input:radio[id$='rbSA']:checked").val();
  var Row6 = $("input:radio[id$='rbNA']:checked").val();
 }
</script>

添加"OnClientClick"GetAge()事件以提交按钮

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" OnClientClick"GetAge()" />