在ASP.NET C#中基于组名从单选按钮组中检索值 [英] Retrieving value from radio button group based on group name in ASP.NET C#

查看:81
本文介绍了在ASP.NET C#中基于组名从单选按钮组中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,通过在asp.net c#中使用linq到sql方法从数据库获取值,动态显示多项选择问题和选项,但是我不知道如何在点击选项后检索值< br $> b $ b

我尝试过:



I create a webpage which will be display multiple choice questions and options with radio buttons dynamically by getting value from database using linq to sql method in asp.net c# but i don't know how to retrieve value after click the option

What I have tried:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TalentTestData.DAL;

namespace TalentTest
{
    public partial class Test1 : System.Web.UI.Page
    {
        private readonly string _connectionstring = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ReadQuestionsIntoTable();

            }
            
        }    
        private void ReadQuestionsIntoTable()
        {
            try
            {
                using (var context = new ExamDataDataContext(_connectionstring))
                {
                    var question = context.Questions.Select(x => x.Question1).ToList();
                    int rowCnt = question.Count;
                    List<string> Choice_data = new List<string>();
                    var Choice = context.Questions.Select(X => new { X.Choice_1, X.Choice_2, X.Choice_3, X.Choice_4 }).ToList();
                    foreach (var item in Choice)
                    {
                        Choice_data.Add(item.Choice_1);
                        Choice_data.Add(item.Choice_2);
                        Choice_data.Add(item.Choice_3);
                        Choice_data.Add(item.Choice_4);
                    }

                    int count = 0;
                    int choice_count = 0;
                    List<string> group_names = new List<string>();
                    for (int rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
                    {
                        TableRow tr1 = new TableRow();
                        tab.Rows.Add(tr1);
                        TableCell tc = new TableCell { Text = question[count] };
                        tr1.Cells.Add(tc);

                        TableRow tr2 = new TableRow();
                        tab.Rows.Add(tr2);
                        for (int j = 0; j < 4; j++)
                        {
                            // create a cell for the choice
                            TableCell Cell = new TableCell
                            {
                                Width = 1000,
                                // align the choices on the left
                                HorizontalAlign = HorizontalAlign.Left
                            };
                            tr2.Cells.Add(Cell);
                            RadioButton rb = new RadioButton
                            {
                                // assign the radio button to Group + QuestionID
                                GroupName = "Group" + rowCtr.ToString(),
                                Text = Choice_data[choice_count],
                                ID = "Radio",
                                Visible = true
                            };
                            Cell.Controls.Add(rb);
                            choice_count++;
                        }
                        group_names.Add("Group" + rowCtr.ToString());
                        TableRow spacer = new TableRow { Height = 30 };
                        TableCell spacerCell = new TableCell { Height = 30 };
                        spacer.Cells.Add(spacerCell);
                        tab.Rows.Add(spacer);
                        count++;
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception caught:", e);
            }


        }
        
    }
}

推荐答案

这可以是一个很好的例子。

[动态多选类型功能]
This can be a good example to start with.
[Dynamic Multiple choice type functionality]


这篇关于在ASP.NET C#中基于组名从单选按钮组中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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