KyLim0211 Ответов: 1

В C# сделать GridView с радиокнопкой выбранного ответа


protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           DataTable dtquestion = new DataTable("Q1");
           dtquestion.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
           dtquestion.Columns.Add(new DataColumn("Code"));
           dtquestion.Rows.Add(1, "1) Achievement of training objective");
           dtquestion.Rows.Add(1, "2) Level of training achieved");
           dtquestion.Rows.Add(1, "3) Delivery of education");
           dtquestion.Rows.Add(1, "4) Related to your job function");
           question1.DataSource = dtquestion;
           question1.DataBind();
       }

   }
   protected void btn_submit(object sender, EventArgs e)
   {
       /* GET answer for each question,insert DB */
   }


мой javascript допускает только 1 ответ на вопрос
<script type="text/javascript">
    function GridSelectAllColumn(objType, chkName) {
        var oItem = objType.children;
        var theBox = (objType.type == "radio") ? objType : objType.children.item[0];
        var strPart = theBox.id.split("_");
        xState = theBox.checked;
        elm = theBox.form.elements;
        for (i = 0; i < elm.length; i++)
            if (elm[i].type == "radio" && elm[i].id != theBox.id && elm[i].id.indexOf(strPart[3]) > -1)
                elm[i].checked = !xState;
    }
</script>

мой gridview
<asp:GridView runat="server" ID="question1"  CssClass="table table-striped table-bordered table-hover" DataKeyNames="ID" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
            <Columns>
                <asp:BoundField DataField="Code" HeaderText="Question" ItemStyle-ForeColor="White" ItemStyle-HorizontalAlign="Left" ItemStyle-BackColor="graytext" />

                <asp:TemplateField HeaderText="Low">
                    <ItemStyle HorizontalAlign="Center" />
                    <ItemTemplate>
                        <asp:RadioButton runat="server" ID="check1" onclick="javascript:GridSelectAllColumn(this, '1');" />
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="High">
                    <ItemStyle HorizontalAlign="Center" />
                    <ItemTemplate>
                        <asp:RadioButton runat="server" ID="check2" onclick="javascript:GridSelectAllColumn(this, '2');" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
<asp:Button ID="Nominate" Text="Submit" CssClass="btn btn-success" runat="server" OnClick="btn_submit" />


Что я уже пробовал:

попробовал получить ответ на выбор пользователя с помощью
проверка 1. текст
проверка2.текст
но не получилось

1 Ответов

Рейтинг:
1

Karthik_Mahalingam

попробуй вот так

protected void btn_submit(object sender, EventArgs e)
        {
            foreach (GridViewRow row in question1.Rows)
            {
                string code = row.Cells[0].Text;
                bool low = (row.FindControl("check1") as RadioButton).Checked;
                bool high = (row.FindControl("check2") as RadioButton).Checked;

            }
        }